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

Finding location using .geojson file using python

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

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

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

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

( )

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 )

既然您已经有了坐标/点,就可以创建一个 多边形 并与之匀称以获得城市边界的几何表示:

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/

in shapely 是一个特定的 class 来处理点,只有在这个 class 中表示的点才能在 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 值并将它们插入到匀称点 class :

 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) 函数将起作用...