geopandas +flask+json+js总结

1、查询库数据,从数据库postgres中,使用ST_AsGeoJSON提取地理化信息字段,并进行转换为geojson格式:
sql = ‘select ST_AsGeoJSON(geom),* from “CellParament铜陵3GisBand”’
sql = text(sql)
df = pd.read_sql(sql, con=engineSourcesGis)
2、构造json格式数据,在flask中得到的从postgres中的数据,需要通过按照geojson这是要求,做字典化转换,以使得flask能识别,循环语句中的"st_asgeojson"字段需要通过json.loads转成json传入字典。直接通过geopandas读取postgres数据库,用to_json方法不成功:
properties = [‘小区名称’, ‘Ci’]
geojson = {‘type’:‘FeatureCollection’, ‘features’:[]}
for _, row in df.iterrows():
feature = {‘type’:‘Feature’,
‘properties’:{},
‘geometry’:json.loads(row[“st_asgeojson”])}
for prop in properties:
feature[‘properties’][prop] = row[prop]
geojson[‘features’].append(feature)
3、json格式转化,在flask中转换json结构化数据,建议使用pd.io.json.dumps:
geojson=pd.io.json.dumps(geojson)
4、通过return render_template(‘appindex.html’, sidemenu=sidemenu, datebytype=datebytype, score=score, badrecord=badrecord,
zerorecord=zerorecord, zerorecordlen=zerorecordlen,celldrawband=geojson,传参
5、flask html 接收参数:

var cell= document.getElementById(‘cellmap’).getAttribute(‘celldraw’);
alert(cell)
var cellband=JSON.parse(cell);
var myStyle = {
“color”: “#00f”,
“weight”: 3,
“opacity”: 0.5,
};
var streets = L.tileLayer(
“https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}”,
{
attribution:
‘Map data © OpenStreetMap contributors, CC-BY-SA, Imagery © Mapbox’,
id: “mapbox.streets”,
maxZoom: 18,
accessToken:
“pk.eyJ1Ijoicmlkd3luIiwiYSI6ImNqbHRzbjhoajBjNWczcGw5ZDVmOXdkdTkifQ.X4LhnVh6T_-vZEIW7fMo2Q”
}
);
var map = L.map(‘cellmap’).setView([30.925226, 117.798988], 13);
streets.addTo(map);
L.geoJSON(cellband).addTo(map);
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值