python - 使用 python 使用 .geojson 文件查找位置

标签 python geojson shapely

我有一个 map.geojson 文件,其中包含各个城市的区域边界。

如何计算由(纬度,经度)给出的点的区域?

最佳答案

您可以解析geojson并提取点/坐标

 import json 
 data = json.loads(datastring)
 data['features'][0]['geometry'] #Your first point

( How can I parse GeoJSON with Python )

When you load a GeoJSON file using the json library, you get a dict that contains an entry features, which contains the list of features. Each feature in turn consists of a dict, which, among other things, contains an entry geometry. The geometry is a dict containing the entries type and coordinates. So you can traverse your GeoJSON file like this:

import json

with open('test.json') as f:
    data = json.load(f)

for feature in data['features']:
    print feature['geometry']['type']
    print feature['geometry']['coordinates']

( https://gis.stackexchange.com/questions/73768/how-to-convert-geojson-to-python-objects )

现在您已经有了坐标/点,可以用它创建一个多边形,以获得城市边界的几何表示:How to create a Polygon given its Point vertices?

Point-in-Polygon

Now that you have a polygon, determining whether a point is inside it is very easy. There’s 2 ways to do it.

`point.within(polygon)`
`polygon.contains(point)`

point should be an instance of the Point class, and poly is of course an instance of Polygon. within and contains are the converse of each other, so whichever method you use is entirely up to you.

https://streamhacker.com/2010/03/23/python-point-in-polygon-shapely/

shapely 是一个处理点的特定类,只有该类中表示的点才能在 point.within(polygon) 中工作 和polygon.contains(point) 函数。

The point type is implemented by a Point class; curve by the LineString and LinearRing classes; and surface by a Polygon class. Shapely implements no smooth (i.e. having continuous tangents) curves. All curves must be approximated by linear splines. All rounded patches must be approximated by regions bounded by linear splines.

( https://toblerity.org/shapely/manual.html )

因此您必须获取十进制 latlon 值并将它们插入到 shapely 点类中:

 from shapely.geometry import Point
 point = Point(0.0, 0.0)
 q = Point((0.0, 0.0))

( https://toblerity.org/shapely/manual.html#points )

使用这个 point.within(polygon)polygon.contains(point) 函数将起作用...

关于python - 使用 python 使用 .geojson 文件查找位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48263802/

相关文章:

python - 如何在 Beautifulsoup 的 find_all() 函数中过滤没有属性的标签?

leaflet - Geojson/草坪 : merge multiple polygons to one polygon keeping hole

python - 使用 Python 附加 geoJSON 功能?

json - 将 geoJSON 对象直接加载到谷歌地图 v3

python - 给定概率界限的大 O 符号?

python - 如何合并 pandas 数据框中的两列,堆叠在顶部

python - 如何使用项目列表查询 mongodb

python - 如何在 pyspark 中获取 Python 库?

dictionary - 如何从中心点计算矩形的纬度和经度

python - shapely 有信封类吗?