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/LeafletJS/WebGISDemoAngularJS/data/ne.geojson'

filePathRegion = r'D:/Project/JavaScript/LeafletJS/WebGISDemoAngularJS/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))

modifyGeoJSON的更多相关文章

随机推荐

  1. Jenkins高危代码执行漏洞检测/开源漏洞靶场

    漏洞细节可以参看安全客的文章:https://bbs.ichunqiu.com/thread-22507-1-1.html Jenkins-CLI 反序列化代码执行(CVE-2017-1000353) ...

  2. cookie&session的Q&A故事[原理篇]

    引语:cookie和session在网站开发中,起着无可厚非的重要作用,但是我们平时往往都只是通过某种语言作为介质,通过某些接口函数进行cookie和session的操作,而对其原理可能不了解或一知半 ...

  3. mysql升级8.0后项目不能连接问题

    转载简书:https://www.jianshu.com/p/a164d582e5d9 主要是因为驱动配置变了driver中得用com.mysql.cj.jdbc.Driver,多了个cj: url后 ...

  4. .NET手记-HttpClient解析GB2312乱码问题

    最近为App的服务器端卸了个爬虫程序,输出结果时发现出现乱码现象,尝试使用了几个方案发现效果并不太好,最后发现了一个很简单的用法. var result = await client.GetByteA ...

  5. Python3学习笔记 - day1

    前言 本文不是一篇系统的从零开始学习Python的教程,如果你需要从零开始学习Python,廖雪峰的官方网站中Python教程这部分将是比较好的一种选择,如果你英语比较好,也可以在国外的一些网站上找到 ...

  6. 课程四(Convolutional Neural Networks),第四 周(Special applications: Face recognition & Neural style transfer) —— 3.Programming assignments:Face Recognition for the Happy House

    Face Recognition for the Happy House Welcome to the first assignment of week 4! Here you will build ...

  7. 关于JS的一些东西

    1.声明Js代码域    1.在head标签中使用script声明js代码域    <head>        ....        <!--声明js代码域-->       ...

  8. SQL 必知必会·笔记<8>分组数据

    1. 使用GROUP BY子句创建分组 示例: SELECT vend_id, COUNT(*) AS num_prods FROM Products GROUP BY vend_id; 注意 GRO ...

  9. quartz执行两遍问题

    转:http://blog.csdn.net/hejinwei_1987/article/details/49100975 在现在的项目中发现Quartz执行了两次,在网上找到下列解决方案,我是用的第 ...

  10. Java基础系列--ArrayList集合

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/8494618.html 一.概述 ArrayList是Java集合体系中最常使用,也是最简单 ...