WMTSTest.py 7.9 KB
# coding=utf-8
#author:        4N
#createtime:    2021/8/18
#email:         nheweijun@sina.com


import random
from app.util.component.SliceScheme import SliceScheme
import random
from osgeo.ogr import *
from osgeo import ogr


def create(slice_para,extent,level,output,check_shp=None):



    file = open(output, 'w')
    lxy_extent={}

    for l in range(level[0],level[1]+1):
        row = 0
        col = 0
        minx, miny, maxx, maxy = SliceScheme.get_polygon(slice_para, l, row, col)

        min_row = 0

        while miny > extent[3]:
            row += 1
            minx, miny, maxx, maxy = SliceScheme.get_polygon(slice_para,l, row, col)
            min_row = row

        max_row = min_row
        while maxy >extent[1]:
            max_row = row
            row += 1
            minx, miny, maxx, maxy = SliceScheme.get_polygon(slice_para,l, row, col)


        min_col=0
        while maxx < extent[0]:
            min_col = col
            col += 1
            minx, miny, maxx, maxy = SliceScheme.get_polygon(slice_para,l, row, col)

        max_col=min_col
        while minx < extent[2]:
            max_col = col

            col += 1
            minx, miny, maxx, maxy = SliceScheme.get_polygon(slice_para,l, row, col)

        lxy_extent[l]=[min_row,max_row,min_col,max_col]
    print(lxy_extent)

    count = 0

    while count<10000:
        l = random.choice(range(level[0],level[1]+1))
        y = random.choice(range(lxy_extent[l][0],lxy_extent[l][1]+1))
        x = random.choice(range(lxy_extent[l][2], lxy_extent[l][3] + 1))

        minx, miny, maxx, maxy = SliceScheme.get_polygon(slice_para, l,y, x)

        if check_shp:
            driver: Driver = ogr.GetDriverByName("ESRI Shapefile")
            ds: DataSource = driver.Open(check_shp, 1)
            if not ds:
                raise Exception("打开数据失败!")
            layer: Layer = ds.GetLayer(0)
            feature:Feature = layer.GetFeature(0)
            geo:Geometry = feature.GetGeometryRef()

            ring = ogr.Geometry(ogr.wkbLinearRing)
            ring.AddPoint(minx, miny)
            ring.AddPoint(minx, maxy)
            ring.AddPoint(maxx, maxy)
            ring.AddPoint(maxx, miny)
            ring.AddPoint(minx, miny)

            # Create polygon
            poly = ogr.Geometry(ogr.wkbPolygon)
            poly.AddGeometry(ring)
            if geo.Intersect(poly):
                file.write("{},{},{}\n".format(l, y, x))
                count+=1
        else:
            file.write("{},{},{}\n".format(l,y,x))
            count += 1

if __name__ == '__main__':
    slice_para = {'rows': 256.0, 'cols': 256.0, 'x': -180.0, 'y': 90.0, 'dpi': 96.0,
                  '0': {'scale': 295497593.05875003, 'resolution': 0.7031250000000002},
                  '1': {'scale': 147748796.52937502, 'resolution': 0.3515625000000001},
                  '2': {'scale': 73874398.26468751, 'resolution': 0.17578125000000006},
                  '3': {'scale': 36937199.132343754, 'resolution': 0.08789062500000003},
                  '4': {'scale': 18468599.566171877, 'resolution': 0.043945312500000014},
                  '5': {'scale': 9234299.783085939, 'resolution': 0.021972656250000007},
                  '6': {'scale': 4617149.891542969, 'resolution': 0.010986328125000003},
                  '7': {'scale': 2308574.9457714846, 'resolution': 0.005493164062500002},
                  '8': {'scale': 1154287.4728857423, 'resolution': 0.002746582031250001},
                  '9': {'scale': 577143.7364428712, 'resolution': 0.0013732910156250004},
                  '10': {'scale': 288571.8682214356, 'resolution': 0.0006866455078125002},
                  '11': {'scale': 144285.9341107178, 'resolution': 0.0003433227539062501},
                  '12': {'scale': 72142.9670553589, 'resolution': 0.00017166137695312505},
                  '13': {'scale': 36071.48352767945, 'resolution': 8.583068847656253e-05},
                  '14': {'scale': 18035.741763839724, 'resolution': 4.2915344238281264e-05},
                  '15': {'scale': 9017.870881919862, 'resolution': 2.1457672119140632e-05},
                  '16': {'scale': 4508.935440959931, 'resolution': 1.0728836059570316e-05},
                  '17': {'scale': 2254.4677204799655, 'resolution': 5.364418029785158e-06},
                  '18': {'scale': 1127.2338602399827, 'resolution': 2.682209014892579e-06},
                  '19': {'scale': 563.6169301199914, 'resolution': 1.3411045074462895e-06}}

    # slice_para = {'rows': 256.0, 'cols': 256.0, 'x': -400.0, 'y': 400.0, 'dpi': 96.0,
    #               '0': {'scale': 590995186.11750006, 'resolution': 1.4062500000000004},
    #               '1': {'scale': 295497593.05875003, 'resolution': 0.7031250000000002},
    #               '2': {'scale': 147748796.52937502, 'resolution': 0.3515625000000001},
    #               '3': {'scale': 73874398.26468751, 'resolution': 0.17578125000000006},
    #               '4': {'scale': 36937199.132343754, 'resolution': 0.08789062500000003},
    #               '5': {'scale': 18468599.566171877, 'resolution': 0.043945312500000014},
    #               '6': {'scale': 9234299.783085939, 'resolution': 0.021972656250000007},
    #               '7': {'scale': 4617149.891542969, 'resolution': 0.010986328125000003},
    #               '8': {'scale': 2308574.9457714846, 'resolution': 0.005493164062500002},
    #               '9': {'scale': 1154287.4728857423, 'resolution': 0.002746582031250001},
    #               '10': {'scale': 577143.7364428712, 'resolution': 0.0013732910156250004},
    #               '11': {'scale': 288571.8682214356, 'resolution': 0.0006866455078125002},
    #               '12': {'scale': 144285.9341107178, 'resolution': 0.0003433227539062501},
    #               '13': {'scale': 72142.9670553589, 'resolution': 0.00017166137695312505},
    #               '14': {'scale': 36071.48352767945, 'resolution': 8.583068847656253e-05},
    #               '15': {'scale': 18035.741763839724, 'resolution': 4.2915344238281264e-05},
    #               '16': {'scale': 9017.870881919862, 'resolution': 2.1457672119140632e-05},
    #               '17': {'scale': 4508.935440959931, 'resolution': 1.0728836059570316e-05},
    #               '18': {'scale': 2254.4677204799655, 'resolution': 5.364418029785158e-06},
    #               '19': {'scale': 1127.2338602399827, 'resolution': 2.682209014892579e-06},
    #               '20': {'scale': 563.6169301199914, 'resolution': 1.3411045074462895e-06}}

    # level = [8,15]
    # extent = [112, 22.7, 115, 24.03]

    # level = [12, 17]
    # extent = [111.644, 28.989, 111.7, 29.027]
    # output = "wmts.bat"
    # create(slice_para,extent,level,output)


    slice_para = {'rows': 256.0, 'cols': 256.0, 'x': 10002100, 'y': -4923200, 'dpi': 96.0,
                  '9': {'scale': 1155583.4197265625, 'resolution': 305.7487246334356},
                  '10': {'scale': 577791.7098632812, 'resolution': 152.8743623167178},
                  '11': {'scale': 288895.8549316406, 'resolution': 76.4371811583589},
                  '12': {'scale': 144447.9274658203, 'resolution': 38.21859057917945},
                  '13': {'scale': 72223.96373291015, 'resolution': 19.109295289589724},
                  '14': {'scale': 36111.98186645508, 'resolution': 9.554647644794862},
                  '15': {'scale': 18055.99093322754, 'resolution': 4.777323822397431},
                  '16': {'scale': 9027.99546661377, 'resolution': 2.3886619111987155},
                  '17': {'scale': 4513.997733306885, 'resolution': 1.1943309555993578},
                  '18': {'scale': 2256.9988666534423, 'resolution': 0.5971654777996789}}

    # level = [9, 18]
    # extent = [12366899.680315087, 2387281.267402596, 12870772.570770962, 2807990.6710842005]
    # output = "ghcwmts.bat"
    #
    # create(slice_para,extent,level,output)

    print(SliceScheme.get_polygon(slice_para,13,1488,3564))