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. SQL注入之重新认识

    i春秋作家:anyedt 原文来自:https://bbs.ichunqiu.com/thread-41701-1-1.html 引言 作为长期占据 OWASP Top 10 首位的注入,认识它掌握它 ...

  2. 学生成绩管理系统(C++指针、链表、文件及面向对象的运用)

    学生成绩管理系统 功能页面显示:    实现源码: #include<iostream> #include<fstream> #include<cstring> # ...

  3. thinkpad的E480安装ubuntu后wifi无法使用问题解决

    买了新电脑,安装ubuntu新系统之后,遇到了一个比较麻烦的问题,在ubuntu中,无法使用wifi. 用新产品就是要当小白鼠啊,查了一下资料,发现这个使用的rtl8821ce的wifi芯片,该wif ...

  4. python实现stack并测试

    栈(stack)又名堆栈,它是一种运算受限的线性表.其限制是仅允许在表的一端进行插入和删除运算. 这一端被称为栈顶,相对地,把另一端称为栈底.向一个栈插入新元素又称作进栈.入栈或压栈,它是把新 元素放 ...

  5. Maven - 实例-2-使用本地仓库中的依赖包

    Maven引入构建包的流程 执行mvn compile命令编译源代码,如果编译过程中需要用到其他的包, maven将会在pom.xml文件中查找是否引入该依赖包的坐标. 示例: <depende ...

  6. TCP/IP 笔记 - 防火墙和网络地址转换

    防火墙是位于内部网和外部网之间的屏障,是系统的第一套防线,作用是防止非法用户的进入. 网络地址转换是一种IP数据包通过路由器或防火墙时通过重写来源IP地址或目的地址的技术,可以用来隐藏或保护内部网络, ...

  7. SQL 必知必会·笔记<9>使用子查询

    子查询(subquery),即嵌套在其他查询中的查询. 1. 利用子查询进行过滤 SELECT 语句中,子查询总是从内向外处理.示例: SELECT cust_name, cust_contact F ...

  8. solr入门

    Solr采用Lucene搜索库为核心,提供全文索引和搜索开源企业平台,提供REST的HTTP/XML和JSON的API,如果你是Solr新手,那么就和我一起来入门吧!本教程以solr4.8作为测试环境 ...

  9. 十大经典排序算法的 JavaScript 实现

    计算机领域的都多少掌握一点算法知识,其中排序算法是<数据结构与算法>中最基本的算法之一.排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大 ...

  10. 微信小程序https配置

    先简单说下什么是https,https与http区别 ,以及https的原理 什么是https 在说HTTPS之前先说说什么是HTTP,HTTP就是我们平时浏览网页时候使用的一种协议.HTTP协议传输 ...