1. 程式人生 > >qgis與圖幅實踐

qgis與圖幅實踐

目標:製作以標準圖幅為背景的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:適量網格

 

參考連線:

https://blog.csdn.net/u012130706/article/details/76285650

https://www.cnblogs.com/gispathfinder/p/6087558.html