Skip to content

Instantly share code, notes, and snippets.

@kiichi
Created June 7, 2016 02:24
Show Gist options
  • Select an option

  • Save kiichi/23538a4d083675fbfa6c5d2b80bc8a1d to your computer and use it in GitHub Desktop.

Select an option

Save kiichi/23538a4d083675fbfa6c5d2b80bc8a1d to your computer and use it in GitHub Desktop.
Intersections between different objects using shapely and pyproj. Could be wrapped in geopandas
# Intersections between different objects
# This demo shows intersects function between
# Polygons (Box and Circle)
# Note that applying buffer in (m) after projection happened
#
# 1) Reproject WGS84 to Web Mercator which uses m in UnicodeTranslateError
# 2) Call intersects
from shapely.geometry import Point,Polygon
import pyproj
def proj_to_plane(lat, lon):
srcp = pyproj.Proj(init='epsg:4326') # WGS84: Lat Lng
destp = pyproj.Proj(init='epsg:3857') # Web Mercator which uses m
return pyproj.transform(srcp, destp, lon, lat)
poly = Polygon([
proj_to_plane(40,-90),
proj_to_plane(40,-79),
proj_to_plane(49,-79),
proj_to_plane(49,-90),
proj_to_plane(40,-90)
])
(rprojx,rprojy) = proj_to_plane(44,-88)
pt = Point(rprojx,rprojy)
circle = pt.buffer(300*1000) # 300km circle
result = poly.intersects(circle)
print(result)
@kiichi
Copy link
Copy Markdown
Author

kiichi commented Jun 7, 2016

Another intersection example between a box and circle(s) look like this: https://twitter.com/objectgraph/status/740006995399446528

@kiichi
Copy link
Copy Markdown
Author

kiichi commented Jun 7, 2016

screen shot 2016-06-06 at 10 26 02 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment