GeoServer获取图层选点的数值
作者:互联网
需求
需要把遥感影像分析的结果展示在地图上,该遥感影像是通过算法反演的结果,进行了着色,需要通过经纬度获取相应的数值。Geoserver上发布的图层已经具有地图选点获取灰度值的功能,采用Openlayers实现,我们这里需要把该功能封装成api接口。下图是Geoserver的Openlayers展示界面。
代码
# -*- coding: utf-8 -*-
import warnings
from pyproj import Proj, transform
import requests
import json
from config import GeoServer
warnings.filterwarnings("ignore")
def to_bbox(lon, lat):
c = 50
x, y = transform(Proj(init='EPSG:4326'), Proj(init="EPSG:32650"), lon, lat)
return "{},{},{},{}".format(x - c, y - c, x + c, y + c)
def get_value(lon, lat, workspace, belong, indicator, time):
print(lon, lat, workspace, belong, indicator, time)
try:
layers = "{}:{}_{}_{}".format(workspace, belong, indicator, time)
bbox = to_bbox(lon, lat)
url = "{}/wms?" \
"SERVICE=WMS" \
"&VERSION=1.1.1" \
"&REQUEST=GetFeatureInfo" \
"&FORMAT=image/jpeg" \
"&TRANSPARENT=true" \
"&QUERY_LAYERS={}" \
"&LAYERS={}" \
"&exceptions=application/vnd.ogc.se_inimage" \
"&INFO_FORMAT=application/json" \
"&FEATURE_COUNT=50" \
"&X=50" \
"&Y=50" \
"&SRS=EPSG:32650" \
"&STYLES=" \
"&WIDTH=101" \
"&HEIGHT=101" \
"&BBOX={}".format(GeoServer, layers, layers, bbox)
print(url)
response = requests.get(url, timeout=5)
temp = json.loads(response.text)['features'][0]['properties']['GRAY_INDEX']
if temp < -2:
return 0
else:
return round(temp, 3)
except:
return 0
if __name__ == '__main__':
lon = 116.17356
lat = 34.7716
workspace = "inversion"
belong = "武汉"
indicator = "疫情"
time = "2020-02"
print(get_value(lon, lat, workspace, belong, indicator, time))
最终实现输入相应经纬度获取图层的对应的灰度值。
标签:选点,indicator,belong,lon,workspace,GeoServer,lat,import,图层 来源: https://blog.csdn.net/xbw12138/article/details/110149060