Shapely point geometry in geopandas df to lat/lon columns

If you have the latest version of geopandas (0.3.0 as of writing), and the if df is a GeoDataFrame, you can use the x and y attributes on the geometry column: df[‘lon’] = df.point_object.x df[‘lat’] = df.point_object.y In general, if you have a column of shapely objects, you can also use apply to do what … Read more

Coordinates of the closest points of two geometries in Shapely

The GIS term you are describing is linear referencing, and Shapely has these methods. # Length along line that is closest to the point print(line.project(p)) # Now combine with interpolated point on line p2 = line.interpolate(line.project(p)) print(p2) # POINT (5 7) An alternative method is to use nearest_points: from shapely.ops import nearest_points p2 = nearest_points(line, … Read more

Calculate overlapped area between two rectangles

This type of intersection is easily done by the “min of the maxes” and “max of the mins” idea. To write it out one needs a specific notion for the rectangle, and, just to make things clear I’ll use a namedtuple: from collections import namedtuple Rectangle = namedtuple(‘Rectangle’, ‘xmin ymin xmax ymax’) ra = Rectangle(3., … Read more

Fix invalid polygon in Shapely

I found a solution that works for the specific case given: >>> pp2 = pp.buffer(0) >>> pp2.is_valid True >>> pp2.exterior.coords[:] [(0.0, 0.0), (0.0, 3.0), (3.0, 3.0), (3.0, 0.0), (2.0, 0.0), (0.0, 0.0)] >>> pp2.interiors[0].coords[:] [(2.0, 1.0), (2.0, 2.0), (1.0, 2.0), (1.0, 1.0), (2.0, 1.0)]

Check if geo-point is inside or outside of polygon

Here is a possible solution to my problem. Geographical coordinates must be stored properly. Example np.array([[Lon_A, Lat_A], [Lon_B, Lat_B], [Lon_C, Lat_C]]) Create the polygon Create the point to be tested Use polygon.contains(point) to test if point is inside (True) or outside (False) the polygon. Here is the missing part of the code: from shapely.geometry import … Read more

Make a union of polygons in GeoPandas, or Shapely (into a single geometry)

Note: cascaded_union mentioned in the answer below is superceded by unary_union if GEOS 3.2+ is used – this allows unions on different geometry types, not only polygons. To check your version, >>> shapely.geos.geos_version (3, 5, 1) From the question/answer here, it seems this is called a cascaded_union within shapely: from shapely.ops import cascaded_union polygons = … Read more