转发geoserver的WMS服务需要三个方法,一个是get瓦片参数,返回png图片;一个是get矢量要素参数,返回json;一个是post gml到后台。
一、get png
在geoserver.py中添加一个方法。
import requests
# 获取wmts服务
def wmts(request):
url='http://localhost:8080/geoserver/wms?SERVICE=WMS&VERSION=1.1.0&'+\
'REQUEST=GetMap&FORMAT=image%2Fpng&TRANSPARENT=true&tiled=true&'+\
'LAYERS='+request.GET['LAYERS']+'&exceptions=application%2Fvnd.ogc.se_inimage&singleTile=true&SRS=EPSG%3A4326&STYLES=&WIDTH='+\
request.GET['WIDTH']+'&HEIGHT='+request.GET['HEIGHT']+'&BBOX='+request.GET['BBOX']
print(url)
image_data=requests.get(url=url,stream=True)
return HttpResponse(image_data,content_type='image/png')
在urls.py中添加一个访问连接。
url(r'^getwmts$', geoserver.wmts
访问:
127.0.0.1:5000/getwmts?LAYERS=gismap%3Av6_time_pref_pgn_utf_wgs84_geoserver&WIDTH=1263&HEIGHT=134&BBOX=111.09189783417972%2C39.53658281977804%2C123.49882337000003%2C40.85291537860616
效果:
二、get feature
在geoserver.py中添加一个方法。
# 获取要素服务
def getfeature(request):
url='http://localhost:8080/geoserver/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&FORMAT=image%2Fpng&'+\
'TRANSPARENT=true&QUERY_LAYERS='+request.GET['QUERY_LAYERS']+'&LAYERS='+request.GET['LAYERS']+\
'&exceptions=application%2Fvnd.ogc.se_inimage&INFO_FORMAT=application/json&FEATURE_COUNT=50&X=50&Y=50'+\
'&SRS=EPSG%3A4326&STYLES=&WIDTH=101&HEIGHT=101&BBOX='+request.GET['BBOX']
json_data=requests.get(url=url)
return HttpResponse(json_data,content_type='application/json')
在urls.py中添加一个访问连接。
url(r'^getfeature$', geoserver.getfeature),
访问:
127.0.0.1:5000/getfeature?QUERY_LAYERS=gismap%3Av6_time_pref_pgn_utf_wgs84_geoserver&LAYERS=gismap%3Av6_time_pref_pgn_utf_wgs84_geoserver&BBOX=116.25889429644533%2C40.173365121277484%2C116.25909429644534%2C40.17356512127749
效果:
三、post GML
在geoserver.py中新建一个方法:
# 提交 gml到geoserver
def postgml(request):
ctx ={}
if request.POST:
head = {"Content-Type": "text/xml; charset=UTF-8", "Connection": "close"}
r = requests.post('http://localhost:8080/geoserver/wfs', data=request.POST['gml'], headers=head)
ctx['rlt'] = r.text
return render(request, "postgml.html", ctx)
在urls.py中增加一个链接:
url(r'^postgml$', geoserver.postgml),
在templates文件夹下,新建一个postgml.html的文件:
django post页面{% csrf_token %}
{{ rlt }}
访问:
在文本框中贴如如下内容:
geometry82.143976 37.149243 85.473018 37.109135 85.312581 34.822926 83.307136 34.2614 82.143976 37.149243name_pyname_pyname_chtestnewname_ftx_coory_coorpres_loctype_pytype_chlev_rankbeg_yrbeg_ruleend_yrend_rulenote_idobj_typesys_idgeo_srccompilergecomplrcheckerent_datebeg_chg_tyend_chg_ty
点击提交,效果如下:
Python django转发完geoserver wms服务后,我们接下来要看怎么把前端的内容放到程序中。