欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 与 JavaScript(js)天地图叠加kmz、kml文件

程序员文章站 2022-07-02 08:55:43
...

js 代码

//kmz的存放路径(服务器绝对路径就行),后台需要对其进行发布服务
addKmzLayer('F:/weblayer/kmz/20190618/test.kmz')
//加载kmz到地图
function addKmzLayer(kmz){
	parm = {  kmz:kmz };
	url="/arc_map/UploadKmz/";
	$.get(url, parm, function (r) {
		if (img) {
	        map.removeLayer(img);
	    }
	    if (r.code == 200) {
	    	var data_kmz = r.ret;
	    	var bd = new T.LngLatBounds(
	    	        new T.LngLat(114.010113, 33.977448),
	    	        new T.LngLat(123.483913, 38.812551));
	    	img = new T.ImageOverlay(data_kmz, bd, {opacity: 0.8, alt: "产品图层加载异常,请联系管理员",});
	    	map.addOverLay(img);
	    	
	    } 
	    else{
	    	return;
	    }
	}, 'json').error(function () {
	})
}

python 后台代码

class UploadKmz(APIView):
    """
    :将kmz发布
    """
    def __init__(self):
        self.Url = "http://127.0.0.1/arcgis/sharing/kml?url={} \
                    &model=simple&folders=&outSR=%7B%22wkid%22%3A4326%7D&callback=\
                    dojo.io.script.jsonp_dojoIoScript2._jsonpCallback"
        self.KmlUrl = "http://127.0.0.1/arcgis/sharing/kml?cache=kml/{}"
        self.kmzUrl = "http://127.0.0.1:4065/webgis/{}"
        
    def process(self,data):
        try:
            kmz = data['kmz']
            if kmz[:3] == 'F:/':
                kmz = kmz[11:]
#             kmz = 'kmz/20190905/ssny_V94201_suit_day_山东省1567680475.kmz'
            url = self.kmzUrl.format(kmz)
            url = self.Url.format(url)
    #         request = urllib2.Request(url)
            result = requests.get(url).content
            dicts = json.loads(result.replace(' ', '')[50:-2])
            kmz_url = dicts['groundOverlays'][0]['href']
            return 200,kmz_url
        except Exception:
            return 500, u'产品加载,请联系管理员!'
        
    @API()
    def get(self, request, *args, **kwargs):
        pass