按对角线地理坐标过滤
Filter on diagonal geo coordinates
我正在尝试使用 Python 和 Folium 在地理坐标上过滤我的文件以仅保留位于曼哈顿的坐标。
我试过设置自己的限制:
top_left = [40.806470, -73.973205]
bottom_left = [40.709729, -74.035690]
bottom_right = [40.696715, -73.992431]
top_right = [40.781518, -73.934066]
low_lat = bottom_right[0]
high_lat = top_left[0]
low_lon = top_right[1]
high_lon = bottom_left[1]
df_bad = df.loc[
(df["Point_latitude"] < high_lat) &
(df["Point_latitude"] > low_lat) &
(df["Point_longitude"] > high_lon) &
(df["Point_longitude"] < low_lon)
]
我对这种方法的问题是它包含了我不想包含的纽约市的部分内容。是这样的直方框:
我想像这样过滤我的地图:
有办法吗?或者也许是一个允许我这样做的新图书馆?
谢谢
使用可以使用匀称。 https://pypi.python.org/pypi/Shapely。事实上,您可以使用它创建多边形和其他地理空间特征。您可能不需要任何 df。这是一个使用多边形和随机点的完整示例。您不需要所有这些模块,但我向您展示了您可以通过多种方式解决问题:
import json
import geojson
from shapely.geometry import mapping, shape, Polygon, MultiPoint
import shapely.wkt as wkt
import folium
top_left = [-73.973205, 40.806470]
bottom_left = [-74.035690, 40.709729]
bottom_right = [-73.992431, 40.696715]
top_right = [-73.934066, 40.781518]
coordinates =[(-74, 40.74),(-74, 40.76),(-74, 40.78),(-74, 40.81)]
coordinates_shapely = MultiPoint(coordinates)
# 1. create a polygon:
polyNY_shapely = Polygon([(top_left), (bottom_left), (bottom_right), (top_right)])
# OR
polyNY_json = {
"coordinates": [[top_left, bottom_left, bottom_right, top_right, top_left]],
"type": "Polygon"
}
# 2. create the geojson of the polygon
g1 = wkt.loads(polyNY_shapely.wkt)
g2a = geojson.Feature(geometry=g1)
# OR
g2b = json.dumps(mapping(shape(polyNY_json)))
# 3. create map with polygon and all coordinates
map_osm = folium.Map(location=[40.7, -74.0],zoom_start=12)
folium.GeoJson(
g2a,
style_function=lambda feature: {
'fillColor': '#ffff00',
'color' : 'blue',
'weight' : 2
}).add_to(map_osm)
for cc in coordinates:
folium.Marker(cc[::-1], popup='point '+str(cc)).add_to(map_osm)
map_osm.save('shapelyfolium.html')
# add points to map after filtering
for pp in range(len(list(coordinates_shapely))):
print polyNY_shapely.contains(coordinates_shapely[pp])
if polyNY_shapely.contains(coordinates_shapely[pp]):
folium.Marker(coordinates[pp][::-1], popup='point '+str(pp),icon = folium.Icon(color='red')).add_to(map_osm)
# OR
# if pp.within(polyNY_shapely):
# folium.Marker(row, popup='point '+str(index),icon = folium.Icon(color='red')).add_to(map_osm)
map_osm.save('shapelyfoliumAfterfiltering.html')
我正在尝试使用 Python 和 Folium 在地理坐标上过滤我的文件以仅保留位于曼哈顿的坐标。 我试过设置自己的限制:
top_left = [40.806470, -73.973205]
bottom_left = [40.709729, -74.035690]
bottom_right = [40.696715, -73.992431]
top_right = [40.781518, -73.934066]
low_lat = bottom_right[0]
high_lat = top_left[0]
low_lon = top_right[1]
high_lon = bottom_left[1]
df_bad = df.loc[
(df["Point_latitude"] < high_lat) &
(df["Point_latitude"] > low_lat) &
(df["Point_longitude"] > high_lon) &
(df["Point_longitude"] < low_lon)
]
我对这种方法的问题是它包含了我不想包含的纽约市的部分内容。是这样的直方框:
我想像这样过滤我的地图:
有办法吗?或者也许是一个允许我这样做的新图书馆?
谢谢
使用可以使用匀称。 https://pypi.python.org/pypi/Shapely。事实上,您可以使用它创建多边形和其他地理空间特征。您可能不需要任何 df。这是一个使用多边形和随机点的完整示例。您不需要所有这些模块,但我向您展示了您可以通过多种方式解决问题:
import json
import geojson
from shapely.geometry import mapping, shape, Polygon, MultiPoint
import shapely.wkt as wkt
import folium
top_left = [-73.973205, 40.806470]
bottom_left = [-74.035690, 40.709729]
bottom_right = [-73.992431, 40.696715]
top_right = [-73.934066, 40.781518]
coordinates =[(-74, 40.74),(-74, 40.76),(-74, 40.78),(-74, 40.81)]
coordinates_shapely = MultiPoint(coordinates)
# 1. create a polygon:
polyNY_shapely = Polygon([(top_left), (bottom_left), (bottom_right), (top_right)])
# OR
polyNY_json = {
"coordinates": [[top_left, bottom_left, bottom_right, top_right, top_left]],
"type": "Polygon"
}
# 2. create the geojson of the polygon
g1 = wkt.loads(polyNY_shapely.wkt)
g2a = geojson.Feature(geometry=g1)
# OR
g2b = json.dumps(mapping(shape(polyNY_json)))
# 3. create map with polygon and all coordinates
map_osm = folium.Map(location=[40.7, -74.0],zoom_start=12)
folium.GeoJson(
g2a,
style_function=lambda feature: {
'fillColor': '#ffff00',
'color' : 'blue',
'weight' : 2
}).add_to(map_osm)
for cc in coordinates:
folium.Marker(cc[::-1], popup='point '+str(cc)).add_to(map_osm)
map_osm.save('shapelyfolium.html')
# add points to map after filtering
for pp in range(len(list(coordinates_shapely))):
print polyNY_shapely.contains(coordinates_shapely[pp])
if polyNY_shapely.contains(coordinates_shapely[pp]):
folium.Marker(coordinates[pp][::-1], popup='point '+str(pp),icon = folium.Icon(color='red')).add_to(map_osm)
# OR
# if pp.within(polyNY_shapely):
# folium.Marker(row, popup='point '+str(index),icon = folium.Icon(color='red')).add_to(map_osm)
map_osm.save('shapelyfoliumAfterfiltering.html')