2016-07-09 14 views
0

私は、地理座標でファイルをフィルタリングして、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) 
] 

私の問題は、それが私にはしたくないNYCの部品が含まれていることです。私はこのような私のマップフィルタリングしたい

Before

enter image description here

をそれを行う方法があることは、このようなストレートボックスですか?それとも私がそれを可能にする新しいライブラリですか?

ありがとうございました

+0

かなり確信してfoliumはポリラインをサポートしています。https://github.com/ python-visualization/folium/blob/master/examples/line_example.py – Benjamin

答えて

0

使用することはできます。 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') 

map before filtering map after filtering

+2

こんにちは、このユースケースで使用する方法をもう少し詳しく説明できますか?これは役に立つと思われますが、私はこのケースでどのように適用するのか分かりません。 – nickfrenchy

+1

ポリゴンを作成するにはhttp://toblerity.org/shapely/manual.html#polygonsを使用します。あなたが「私のファイルをフィルタする」と言うとき、私はあなたがそのエリア内にポイントを持っていたいと思っています。その場合は、このチュートリアルhttp://streamhacker.com/2010/03/23/python-point-in-polygon-shapely/に従ってください。それが「自分のファイルをフィルタする」という意味ではない場合は、あなたの質問をより詳しく説明してください。 – giosans

+0

はいポイントをエリアに保存したいと思います。適切なツールのようにはっきりと見えますが、私はそれを使う方法がわかりません。 領域制限座標でポリゴン関数を使用すると、TypeErrorが発生します:__init __()は1から3の位置引数を取りますが、9が与えられました... – nickfrenchy

関連する問題