Package: pgpd
Small wrapper to use pygeos functions from pandas.The main use case is if you want to have geometries in your dataframe,
but you do not care about CRS at all and thus do not need all the extra features from GeoPandas.InstallThis package is available on PyPi for installation.pipinstallpgpdExampleLet's get started by first creating a dataframe with PyGEOS data.Note that we need to explicitly set the type of the PyGEOS columns to"geos"!>>>importpandasaspd>>>importpygeos>>>importpgpd>>># Create a DataFrame>>>df=pd.DataFrame({...'a':list('abcde'),...'poly':pygeos.box(range(5),0,range(10,15),10),...'pt':pygeos.points(range(5),range(10,15))...})>>>df=df.astype({'poly':'geos','pt':'geos'})>>>dfapolypt0aPOLYGON((00,010,1010,100,00))POINT(010)1bPOLYGON((10,110,1110,110,10))POINT(111)2cPOLYGON((20,210,1210,120,20))POINT(212)3dPOLYGON((30,310,1310,130,30))POINT(313)4ePOLYGON((40,410,1410,140,40))POINT(414)SeriesWe can access pygeos functionality through the "geos" accessor namespace.>>>df.poly.geos.length()040.0140.0240.0340.0440.0Name:length,dtype:float64>>>df.pt.geos.total_bounds()xmin0.0ymin10.0xmax4.0ymax14.0Name:total_bounds,dtype:float64>>>df.poly.geos.clip_by_rect(0,0,5,10)0POLYGON((00,010,510,50,00))1POLYGON((10,110,510,50,10))2POLYGON((20,210,510,50,20))3POLYGON((30,310,510,50,30))4POLYGON((40,410,510,50,40))Name:clip_by_rect,dtype:geosSome functions return more values per row, so we convert them to DataFrames:>>>df.poly.bounds()xminyminxmaxymax00.00.010.010.011.00.011.010.022.00.012.010.033.00.013.010.044.00.014.010.0There are some functions that return a variable number of items per original object.
For these functions, the index of the returned Series/DataFrame will point to the original object index.>>>points=pd.Series(...pygeos.multipoints(...[[0,0],[1,1],[2,2],[0,1],[2,3],[10,20],[30,40],[40,50],[50,60]],...indices=[0,0,0,1,1,2,2,2,2],...),...dtype='geos',...)>>>points0MULTIPOINT(00,11,22)1MULTIPOINT(01,23)2MULTIPOINT(1020,3040,4050,5060)dtype:geos>>>points.geos.get_parts()0POINT(00)0POINT(11)0POINT(22)1POINT(01)1POINT(23)2POINT(1020)2POINT(3040)2POINT(4050)2POINT(5060)Name:get_parts,dtype:geos>>>points.geos.get_coordinates()xyz00.00.0NaN01.01.0NaN02.02.0NaN10.01.0NaN12.03.0NaN210.020.0NaN230.040.0NaN240.050.0NaN250.060.0NaNFinally, PyGEOS also has some binary functions, which work on 2 different sets of geometries.These functions are also made available on Series, but work slightly differently.
We added amannerargument, which can be one of 3 different values:keep,align,expand.
This argument dictates how the 2 sets of geometries are transformed before running the binary function:keep: Function is run on the input as is.align: Align both sets with each other, according to their index (only works whenotheris a Series).expand: Expand both sets to a 2D array and compare each geometry of set A with each geometry of set B (returns a 2D array of dimension<len(A), len(B)>).>>># KEEP: Just runs the `contains` function on the "poly" column data and the given Point>>>df.poly.geos.contains(pygeos.Geometry("Point (11 5)"),manner='keep')0False1False2True3True4TrueName:contains,dtype:bool>>># ALIGN: We only pass 3 points, but tell the function to align the data according to the index>>>df.poly.geos.distance(df.pt[1:4],manner='align')0NaN11.022.033.04NaNName:distance,dtype:float64>>># EXPAND: Compare each polygon with each point (returns numpy.ndarray <5,3> in this case)>>>df.poly.geos.distance(df.pt[1:4],manner='expand')array([[1.,2.,3.],[1.,2.,3.],[1.41421356,2.,3.],[2.23606798,2.23606798,3.],[3.16227766,2.82842712,3.16227766]])One last difference is that you can omit theotherset of geometries.
The method will then automatically choose theexpandmode and use theselfdata twice.>>># Compute all possible intersection areas of the geometries in the "poly" column>>>pygeos.area(df.poly.geos.intersection())array([[100.,90.,80.,70.,60.],[90.,100.,90.,80.,70.],[80.,90.,100.,90.,80.],[70.,80.,90.,100.,90.],[60.,70.,80.,90.,100.]])DataFrameWhile all PyGEOS functions are available on Series, some are made available on the DataFrame as well.The functions that are available on DataFrames are those that have a 1-to-1 mapping (create one output for each geometry in the column), or those that have a fixed number of outputs for the entire geos column.>>># Fixed number of outputs (ic. xmin,ymin,xmax,ymax)>>>df.geos.total_bounds()polyptxmin0.00.0ymin0.010.0xmax14.04.0ymax10.014.0>>># For every PyGEOS function that has a 1-to-1 relation,>>># the DataFrame variant allows inplace modification>>>df.geos.apply(lambdacoord:coord*2,inplace=True)>>>dfapolypt0aPOLYGON((00,020,2020,200,00))POINT(020)1bPOLYGON((20,220,2220,220,20))POINT(222)2cPOLYGON((40,420,2420,240,40))POINT(424)3dPOLYGON((60,620,2620,260,60))POINT(626)4ePOLYGON((80,820,2820,280,80))POINT(828)GeoPandasThe main use case for this library is not to depend on GeoPandas and all of its dependencies.
However -if you need to- this library provides methods to convert from and to GeoPandas.Series>>>gs=df.pt.geos.to_geopandas(crs='WGS84')>>>gs0POINT(0.0000020.00000)1POINT(2.0000022.00000)2POINT(4.0000024.00000)3POINT(6.0000026.00000)4POINT(8.0000028.00000)Name:pt,dtype:geometry>>>s2=gs.geos.from_geopandas()>>>s20POINT(020)1POINT(222)2POINT(424)3POINT(626)4POINT(828)Name:pt,dtype:geosDataFrame>>>gdf=df.geos.to_geopandas(geometry='poly',crs='WGS84')>>>gdfapolypt0aPOLYGON((0.000000.00000,0.0000020.00000,2...POINT(020)1bPOLYGON((2.000000.00000,2.0000020.00000,2...POINT(222)2cPOLYGON((4.000000.00000,4.0000020.00000,2...POINT(424)3dPOLYGON((6.000000.00000,6.0000020.00000,2...POINT(626)4ePOLYGON((8.000000.00000,8.0000020.00000,2...POINT(828)>>>gdf.dtypesaobjectpolygeometryptgeosdtype:object>>>df2=gdf.geos.from_geopandas()>>>df2apolypt0aPOLYGON((00,020,2020,200,00))POINT(020)1bPOLYGON((20,220,2220,220,20))POINT(222)2cPOLYGON((40,420,2420,240,40))POINT(424)3dPOLYGON((60,620,2620,260,60))POINT(626)4ePOLYGON((80,820,2820,280,80))POINT(828)>>>df2.dtypesaobjectpolygeosptgeosdtype:object