pylusat.distance module

pylusat.distance.to_point(input_gdf, point_gdf, method='euclidean', dtype=<class 'float'>)

Calculate distance (euclidean or manhattan) for each geometry in the input GeoDataFrame to its nearest neighbor in the point GeoDataFrame.

Parameters:
  • input_gdf (geopandas.GeoDataFrame) – Input GeoDataFrame. Centroids of the input geometries are used.

  • point_gdf (geopandas.GeoDataFrame) – The GeoDataFrame contains the point geometries to which distances are calculated.

  • method (str, optional) – Method used to calculate distances. Either ‘euclidean’ or ‘manhattan’.

  • dtype (str or numpy.dtype, optional) – Use a np.dtype or Python type to cast the output distance to the desired type.

Returns:

A pandas Series containing the distances of each input feature to its nearest point.

Return type:

pandas.Series

Examples

Calculate (Euclidean) distance to points (schools) with polygons (census block groups):

>>> pylusat.distance.to_point(acs2016_gdf, schools_gdf)
0   197.284083
1   721.557482
2   529.379113
3   293.479326
4   186.180728
...
150 1254.314693
151 471.434822
152 793.974181
153 2279.119749
154 500.748225
pylusat.distance.to_line(input_gdf, line_gdf, cellsize=30, method='euclidean', dtype=<class 'float'>)

Calculate distances from input_gdf to line_gdf.

Parameters:
  • input_gdf (geopandas.GeoDataFrame) – Input GeoDataFrame. Centroids of the input geometries are used.

  • line_gdf (geopandas.GeoDataFrame) – A GeoDataFrame whose geometry is of line.

  • cellsize (float) – Cell size used to rasterize the line_gdf.

  • method (str, optional) – Method used to calculate distances. Either ‘euclidean’ or ‘manhattan’.

  • dtype (str or numpy.dtype, optional) – Use a np.dtype or Python type to cast the output distance to the desired type.

Returns:

A pandas Series of distances from each feature in input_gdf to its nearest neighbor in line_gdf.

Return type:

pandas.Series

Notes

To rapidly query distances, the line_gdf is burned into numpy array by using rasterize function from the rasterio package.

Examples

Calculate Euclidean distance to lines (highways) with polygons (census block groups):

>>> pylusat.distance.to_line(acs2016_gdf, highway_gdf)
0   715.611627
1   324.499615
2   1020.00000
3   150.000000
4   192.093727
...
150 1194.738465
151 2753.633963
152 900.0000000
153 2036.909424
154 778.845299
pylusat.distance.to_cell(input_gdf, raster, value, nodata=None, method='euclidean', dtype=<class 'float'>)

Calculate distance for each geometry to its nearest-neighbor cell that has a specific value.

Parameters:
  • input_gdf (geopandas.GeoDataFrame) – Input GeoDataFrame

  • raster (str) – A path to a tif file or a connection string to a raster on PostgreSQL.

  • value (int or float) – Cells in the raster with this value will be used as targets for distance calculation.

  • nodata (int or float) – Value for no data cells.

  • method (str, optional, default "euclidean") – Method used to calculate distances. Either ‘euclidean’ or ‘manhattan’.

  • dtype (str or numpy.dtype, optional) – Use a numpy.dtype or Python type to cast the output distance to the desired type.

Returns:

A pandas Series of distances from each feature in input_gdf to the nearest cell (has the specified value) in the raster dataset.

Return type:

pandas.Series

Examples

Calculate distance from census block groups (acs2016) to nearest-neighbor cells (habitat raster) with cell value 6 as the distance target.

>>> pylusat.distance.to_cell(acs2016_gdf, habitat_tif, 6)
0   5825.409867
1   4953.271646
2   5031.600143
3   4206.851554
4   2808.647361
...
150 8111.547325
151 8481.143791
152 9688.188685
153 4740.854353
154 4250.799925