目标:制作以标准图幅为背景的shp文件,图幅由网格组成,网格内显示图幅号
成果:
方法:
1、首先生成图幅坐标文件csv,见代码片段;
2、通过qgis,加载csv图层;
3、qgis:矢量->研究工具->网格,按照左上角、右下角的坐标,间隔度,生成网格;
4、这里已经初见效果,为了更好操作,可以叠加融合处理,qgis:矢量->地学处理工具->联合,合并两图层(正在试验)
生成图幅左上角位置代码:
# -*- coding: utf-8 -*-
#!/usr/local/bin/python
import math
meshRow = "A,B,C,D,E,F,G,H,I,J,K,L,M,N".split(",")
meshList = [43,44,45,46,47,48,49,50,51,52,53]
meshCode = {"B": 4, "C": 16, "D": 144, "E": 576, "F": 2304,"E":9216}
meshDeLan = 6.0 #0.0625
meshDeLat = 4.0 #0.0416666666667
#72,138,0,56
#3/2
def getMesh100W():
meshs = {};
for i in range(len(meshRow)):
for j in range(len(meshList)):
topLeft = ((meshList[j]-1) * meshDeLan - 180, (i+1) * meshDeLat)
meshs[meshRow[i] + str(meshList[j])] = topLeft
return meshs;
def getMeshCode(code):
meshs100W = getMesh100W()
meshNum = int(math.sqrt(meshCode[code]))
meshs = {}
for m,topLeft in meshs100W.items():
lngD = meshDeLan / meshNum
latD = meshDeLat / meshNum
for i in range(meshNum):
for j in range(meshNum):
s = m + code + str(i + 1).rjust(3,'0') + str(j + 1).rjust(3,'0')
meshs[s] = (topLeft[0]+lngD*j+lngD/2, topLeft[1]-latD*i-latD/2)
return meshs;
if __name__ == "__main__":
f = open("meshListF.csv","w")
meshs = getMeshCode("F")
f.write("mesh,lan,lat\n")
for k,v in meshs.items():
f.write(k+","+str(v[0])+","+str(v[1])+"\n")
JavaScript代码:想用高德sdk自定义图层呢,太慢,需要优化
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>自定义图层</title>
<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
<script src="https://webapi.amap.com/maps?v=1.4.10&key=自己申请"></script>
<script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
<script type="text/javascript" src="mesh.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/javascript">
var map, canvas, context;
// create(); //自定义图层
function create() {
map = new AMap.Map('container', {
center: [116.306206, 39.975468],
zoom:8
});
map.plugin(['AMap.CustomLayer'], function() {
canvas = document.createElement('canvas');
canvas.width = map.getSize().width;
canvas.height = map.getSize().height;
context = canvas.getContext('2d');
var cus = new AMap.CustomLayer(canvas, {
zooms: [5, 8],
zIndex: 12
});
cus.render = onRender;
cus.setMap(map);
});
}
var meshRow = "A,B,C,D,E,F,G,H,I,J,K,L,M,N".split(",");
var meshList = [43,44,45,46,47,48,49,50,51,52,53];
var meshCode = {"B": 4, "C": 16, "D": 144, "E": 576, "F": 2304};
var meshDeLat = 4;
var meshDeLan = 6;
var meshs = [];
function onRender() {
context.clearRect(0, 0, canvas.width, canvas.height)
if (map.getZoom() < 8){
meshs = getMesh100W();
}else{
meshs = getMeshCode("C");
}
for(m in meshs){
drawMesh(map, m, meshs[m]);
}
}
function getMesh100W() {
meshs = [];
for (i = 0; i< meshRow.length; i++) {
for (j = 0; j< meshList.length; j++) {
var topLeft = new AMap.LngLat((meshList[j]-1) * meshDeLan - 180, (i+1) * meshDeLat);
var downRight = new AMap.LngLat(meshList[j] * meshDeLan - 180, i * meshDeLat);
meshs[meshRow[i] + meshList[j]] = new AMap.Bounds(topLeft, downRight);
}
}
return meshs;
}
function getMeshCode(code) {
var meshs100W = getMesh100W();
var meshNum = Math.sqrt(meshCode[code]);
meshs = [];
for (m in meshs100W) {
var topLeft = meshs100W[m].getSouthWest();
var downRight = meshs100W[m].getNorthEast();
var lngD = (downRight.getLng() - topLeft.getLng()) / meshNum;
var latD = (topLeft.getLat() - downRight.getLat()) / meshNum;
for (i=0; i< meshNum; i++){
for (j=0; j< meshNum; j++){
s = m + code + meshName((i + 1),3) + meshName((j + 1),3);
meshs[s] = new AMap.Bounds(new AMap.LngLat(topLeft.getLng()+lngD*j, topLeft.getLat()-latD*i), new AMap.LngLat(topLeft.getLng()+lngD*(j+1), topLeft.getLat()-latD*(i+1)));
}
}
}
return meshs;
}
function drawMesh(map, name, bound) {
var gps = [bound.getSouthWest(), bound.getNorthEast(), bound.getCenter()];
AMap.convertFrom(gps, 'gps', function (status, result) {
if (result.info === 'ok') {
var lnglats = result.locations;
var topLeft = map.lngLatToContainer(lnglats[0]);
var downRight = map.lngLatToContainer(lnglats[1]);
var center = map.lngLatToContainer(lnglats[2]);
context.font="15px Verdana";
context.fillStyle="#ff0000";
context.strokeStyle="#FF0000";
context.strokeRect(topLeft.x, topLeft.y, downRight.x - topLeft.x, downRight.y - topLeft.y);
context.fillText(name, center.x, center.y);
}
});
}
function meshName(num, formNum){
name = "000000" + num;
return name.substr(name.length - formNum);
}
</script>
</body>
</html>
qgis:适量网格
参考连接: