将地质图(shp文件)叠加到天地图上

1.环境依赖

python2.7+pyshp模块
下载python2.7

2.配置环境

安装时可以直接add path。
在cmd中输入python,出现以下界面代表配置成功。
手动配置环境变量教程
网上有很多教程可以去搜搜。
将地质图(shp文件)叠加到天地图上_第1张图片
环境配置好后,在cmd中输入python -m pip install pyshp -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com安装pyshp模块。
至此环境就配置好了。

3.主要代码

首先到开发目录开启服务器python -m CGIHTTPServer 8889,在浏览器中输入localhost:8889
在你的开发目录新建index.html文件


<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<meta name="keywords" content="天地图"/>
<title>天地图-地图API-范例-经纬度直投地图title>
<script type="text/javascript" src="http://api.tianditu.gov.cn/api?v=4.0&tk=您的密钥">script>
<style type="text/css">body,html{width:100%;height:100%;margin:0;font-family:"Microsoft YaHei"}#mapDiv{width:100%;height:400px}input,b,p{margin-left:5px;font-size:14px}style> 
<script type="text/javascript" src="http://localhost:8889/cgi-bin/readShp.py">script>
head>
<body onLoad="onLoad()">
<div id="mapDiv">div>
body>
html>

在你的开发目录新建一个文件cgi-bin,写的py文件必须放在这个目录。
readShp.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: wardseptember
print "Content-Type:text/javascript"
print


import shapefile
import sys
reload(sys)
sys.setdefaultencoding('utf-8') 
sf = shapefile.Reader("./cgi-bin/shapefile/aotao.shp")
shapes = sf.shapes()

first_str = '''
	var map;
    var zoom = 9;
    map = new T.Map('mapDiv', {
        projection: 'EPSG:32650'
    });
    map.centerAndZoom(new T.LngLat(112.858577, 35.497694), zoom);

'''
name_str=''''''
for num in range(len(shapes)):
	name_str=name_str+'''
		points_autao%d=[];
	'''% (num)

first_str=first_str+name_str

polyline_str = ''''''
for shp in range(len(shapes)):
    shap = shapes[shp]#一个面文件,里面可能有很多ploygon,shapes[shp]就是一个,一共有len(shapes)个ploygon
    for i in range(len(shap.points)):#读每个ploygon中点坐标
        polyline_str=polyline_str+'''
            points_autao%d.push(new T.LngLat(%f, %f));
        '''% (shp,shap.points[i][0],shap.points[i][1])


finally_str=''
for num in range(len(shapes)):
	finally_str=finally_str+'''
	    var polygon_autao%d = new T.Polygon(points_autao%d,{
	                color: "blue", weight: 3, opacity: 0.5, fillColor: "red", fillOpacity: 0.5
	            });
	    map.addOverLay(polygon_autao%d);
	'''% (num,num,num)

print first_str+polyline_str+finally_str
#必须要挨个读取ploygon,不能一次读取,不然画出来的图形就乱了。一次压入所有点,就依点画,明显不对。

读其他ploygon图层写法

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: wardseptember

print "Content-Type:text/javascript"
print


import shapefile
import sys
reload(sys)
sys.setdefaultencoding('utf-8') 
sf = shapefile.Reader("./cgi-bin/shapefile/sandie.shp")
shapes = sf.shapes()

first_str = '''
'''
name_str=''''''
for num in range(len(shapes)):
	name_str=name_str+'''
		points_sd%d=[];
	'''% (num)

first_str=first_str+name_str

polyline_str = ''''''
for shp in range(len(shapes)):
    shap = shapes[shp]
    for i in range(len(shap.points)):
        polyline_str=polyline_str+'''
            points_sd%d.push(new T.LngLat(%f, %f));
        '''% (shp,shap.points[i][0],shap.points[i][1])


finally_str=''
for num in range(len(shapes)):
	finally_str=finally_str+'''
	    var polygon_sd%d = new T.Polygon(points_sd%d,{
	                color: "blue", weight: 3, opacity: 0.5, fillColor: "#37FA3F", fillOpacity: 0.5
	            });
	    map.addOverLay(polygon_sd%d);
	'''% (num,num,num)

print first_str+polyline_str+finally_str

读线文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: wardseptember
print "Content-Type:text/javascript"
print


import shapefile
import sys
reload(sys)
sys.setdefaultencoding('utf-8') 
sf = shapefile.Reader("./cgi-bin/shapefile/JCborder.shp")
shapes = sf.shapes()
first_str = '''points = [];'''
polyline_str = ''''''
for shp in range(len(shapes)):
    shap = shapes[shp]
    for i in range(len(shap.points)):
        polyline_str=polyline_str+'''
            points.push(new T.LngLat(%f, %f));
        '''% (shap.points[i][0],shap.points[i][1])
finally_str='''
    var line = new T.Polyline(points);
    map.addOverLay(line);
'''

print first_str+polyline_str

可以发现读线读点读面写法类似。
下面是批量读点的代码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author: wardseptember
print "Content-Type:text/javascript"
print


import shapefile
import sys
reload(sys)
sys.setdefaultencoding('utf-8') 
sf = shapefile.Reader("./cgi-bin/shapefile/JCborder.shp")
shapes = sf.shapes()

polyline_str = ''''''
lng_num=[0.0]
lat_num=[0.0]
total_points=1
for shp in range(len(shapes)):
    shap = shapes[shp]
    for i in range(len(shap.points)):
    	total_points=total_points+1
    	lng_num.append(shap.points[i][0])
    	lat_num.append(shap.points[i][1])


for j in range(total_points):
    polyline_str=polyline_str+'''
        var point_%d=new T.LngLat(%f, %f);
        var marker_%d = new T.Marker(point_%d);
        map.addOverLay(marker_%d);
        marker_%d.disableDragging();  
    '''% (j,lng_num[j],lat_num[j],j,j,j,j)

polyline_str=polyline_str+'map.removeOverLay(marker_0)'
print polyline_str

将地质图(shp文件)叠加到天地图上_第2张图片

你可能感兴趣的:(python学习笔记)