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);
})