zoukankan      html  css  js  c++  java
  • 应用Python处理空间关系数据

    from osgeo import ogr
    import json
    from geojson import loads, dumps, Feature, FeatureCollection
    from shapely.geometry import shape, Point, LineString

    '''
    shp_driver = ogr.GetDriverByName('ESRI Shapefile')
    shp_dataset = shp_driver.Open(r'../geodata/schools.shp')
    shp_layer = shp_dataset.GetLayer()
    shp_srs = shp_layer.GetSpatialRef()
    '''

    filePathNE = r'D:/Project/JavaScript/data/ne.geojson'
    filePathRegion = r'D:/Project/JavaScript/data/region.geojson'


    def readGeoJSONFileToGeoJSON(jsonfile):
        with open(jsonfile) as jsonFile:
            jsonStr = jsonFile.read()
            featureCollection = loads(jsonStr)
            #print(dumps(featureCollection))
            return featureCollection

    def readGeoJSONFileToJSONObject(jsonfile):
        with open(jsonfile) as jsonFile:
            jsonObject = json.load(jsonFile)
            return jsonObject

    #
    def JSONObjectToShape(jsonObject):
        geometryList = []
        for feature in jsonObject['features']:
            #将GeoJSON中的Geometry转化成shapely(Geos)中的Geometry
            # create shapely shape from geojson
            shapeObj = shape(feature['geometry'])
            geometryList.append(shapeObj)
            #feature['geometry'] = None
        return geometryList

    jsonObjectNE = readGeoJSONFileToJSONObject(filePathNE)
    geometryNEList = JSONObjectToShape(jsonObjectNE)
    jsonObjectRegion = readGeoJSONFileToJSONObject(filePathRegion)
    geometryRegionList = JSONObjectToShape(jsonObjectRegion)

    for indexRegion, region in enumerate(geometryRegionList):
        for indexNE, ne in enumerate(geometryNEList):
            isIntersect = region.intersects(ne)
            if isIntersect:
                featureNE = jsonObjectNE['features'][indexNE]
                featureRegion = jsonObjectRegion['features'][indexRegion]
                featureNE['properties']['region'] = featureRegion['properties']['name']
                #if hasattr(featureRegion['properties'], 'count'):
                if featureRegion['properties'].get('count', None) is not None:              
                    featureRegion['properties']['count'] = featureRegion['properties']['count'] + ',' + str(featureNE['properties']['count'])
                else:
                    featureRegion['properties']['count'] = str(featureNE['properties']['count'])

    print(json.dumps(jsonObjectRegion))

  • 相关阅读:
    csharp: Cyotek.GhostScript.PdfConversion pdf convert image
    csharp: using Acrobat.dll pdf convert images in winform
    机器学习实战---K均值聚类算法
    机器学习实战---决策树CART回归树实现
    机器学习实战---决策树CART简介及分类树实现
    机器学习实战---线性回归(更好的使用正规方程求解)
    机器学习实战---逻辑回归梯度上升(更好的理解sigmoid函数的含义并改进)
    机器学习实战---朴素贝叶斯算法使用K折交叉验证
    机器学习实战---朴素贝叶斯算法
    机器学习实战---决策树ID3算法
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/6107886.html
Copyright © 2011-2022 走看看