应用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))
相关文章
- 自动分析局域网内网速慢的电脑---结合IPERF,TASK SCHEDULE,PYTHON,MAIL
- python的dict,set,list,tuple应用详解
- Python 日期和时间_python 当前日期时间_python日期格式化
- Python视觉深度学习系列教程 第二卷 第3章 理解rank-1&rank-5精度
- Python语言学习:Python语言学习之逻辑控制语句(if语句&for语句&while语句&range语句&with语句)的简介、案例应用之详细攻略
- Python语言学习:Python语言学习之正则表达式相关(re正则表达式库)的简介、常用函数、案例应用之详细攻略
- Python语言学习之文件夹那些事:python和文件夹的使用方法之详细攻略
- Python语言学习:Python语言学习之迭代/递归/OS输入输出/错误&异常处理的简介、案例应用之详细攻略
- Python语言学习:Python语言学习之python包/库package的简介(模块的封装/模块路径搜索/模块导入方法/自定义导入模块实现华氏-摄氏温度转换案例应用)、使用方法、管理工具之详细攻略
- Python语言学习:Python语言学习之列表/元祖/字典/集合的简介、案例应用之详细攻略
- Python编程语言学习:python中浅复制/深复制(或浅拷贝/深拷贝)的简介、案例应用注意事项之详细攻略
- Python编程语言学习:python的列表的特殊应用之一行命令实现if判断中的两类判断
- Ubuntu下完美切换Python版,即设置系统默认的python版本(亲测有效)
- Python:Jinja2模板语法整理
- python 调用图灵机器人api实现简单的人机交互
- 8个Python工具,用来做应用程序开发真的很好用
- python实战===一行代码就能搞定的事情!
- python基础===八大排序算法的 Python 实现
- python如何实现文件下载进度条呢?
- OpenCV-Python学习(6)—— OpenCV 图像算术操作(cv.add、cv.subtract、cv.multiply、cv.divide)