spatial_functions.py

lib.spatial_functions.array2raster(newRasterfn, array, rasterOrigin, param)

This function saves array to geotiff raster format (used in cutting with shapefiles).

Parameters
  • newRasterfn (string) – Output path of the raster.

  • array (numpy array) – Array to be converted into a raster.

  • rasterOrigin (list of two floats) – Latitude and longitude of the Northwestern corner of the raster.

  • param (dict) – Dictionary of parameters including GeoRef and CRS.

Returns

The raster file will be saved in the desired path newRasterfn.

Return type

None

lib.spatial_functions.array_to_raster(array, destination_file, input_raster_file)

This function changes from array back to raster (used after kmeans algorithm).

Parameters
  • array (numpy array) – The array which needs to be converted into a raster.

  • destination_file (string) – The file name where the created raster file is saved.

  • input_raster_file (string) – The original input raster file from which the original coordinates are taken to convert the array back to raster.

Returns

The raster file will be saved in the desired path destination_file.

Return type

None

lib.spatial_functions.assign_disconnected_components_to_nearest_neighbor(data, w)

This loop is used to force any disconnected group of polygons (graph component) to be assigned to the nearest neighbors.

Parameters
  • data (geodataframe) – The geodataframe of polygons to be clustered.

  • w (pysal weights object) – The pysal weights object of the graph (w.neighbors is similar to an adjacency matrix).

Return w

The updated pysal weights objected is returned.

Return type

pysal weights object

lib.spatial_functions.calc_geotiff(Crd_all, res_desired)

This function returns a dictionary containing the georeferencing parameters for geotiff creation, based on the desired extent and resolution.

Parameters
  • Crd_all (numpy array) – Coordinates of the bounding box of the spatial scope.

  • res_desired (list) – Desired data resolution in the vertical and horizontal dimensions.

Return GeoRef

Georeference dictionary containing RasterOrigin, RasterOrigin_alt, pixelWidth, and pixelHeight.

Return type

dict

lib.spatial_functions.calc_region(region, Crd_reg, res_desired, GeoRef)

This function reads the region geometry, and returns a masking raster equal to 1 for pixels within and 0 outside of the region.

Parameters
  • region (Geopandas series) – Region geometry

  • Crd_reg (list) – Coordinates of the region

  • res_desired (list) – Desired high resolution of the output raster

  • GeoRef (dict) – Georeference dictionary containing RasterOrigin, RasterOrigin_alt, pixelWidth, and pixelHeight.

Return A_region

Masking raster of the region.

Return type

numpy array

lib.spatial_functions.ckd_nearest(gdf_a, gdf_b, bcol)

This function finds the distance and the nearest points in gdf_b for every point in gdf_a.

Parameters
  • gdf_a (geodataframe) – GeoDataFrame of points, forming a component that is disconnected from gdf_b.

  • gdf_b (geodataframe) – GeoDataFrame of points, forming a component that is disconnected from gdf_a.

  • bcol (string) – Name of column that should be listed in the resulting DataFrame.

Return df

Dataframe with the combinations of pair of points as rows, and 'distance' and 'bcol' as columns.

Return type

pandas dataframe

lib.spatial_functions.crd_bounding_box(Crd_regions, resolution)

This function calculates coordinates of the bounding box covering data in a given resolution.

Parameters
  • Crd_regions (numpy array) – Coordinates of the bounding boxes of the regions.

  • resolution (numpy array) – Data resolution.

Return Crd

Coordinates of the bounding box covering the data for each region.

Return type

numpy array

lib.spatial_functions.ind_from_crd(Crd, Crd_all, res)

This function converts longitude and latitude coordinates into indices within the spatial scope of the data.

Parameters
  • Crd (numpy array) – Coordinates to be converted into indices.

  • Crd_all (numpy array) – Coordinates of the bounding box of the spatial scope.

  • res (list) – Resolution of the data, for which the indices are produced.

Return Ind

Indices within the spatial scope of data.

Return type

numpy array

lib.spatial_functions.polygonize_raster(input_file, output_shapefile, column_name)

This function is used to change from a raster to polygons as max-p algorithm only works with polygons.

Parameters
  • input_file (string) – The path to the file which needs to be converted to a polygon from a raster.

  • output_shapefile (string) – The path to the shapefile which is generated after polygonization.

  • column_name (string) – The column name, the values from which are used for conversion.

Returns

The shapefile of (multi)polygons is saved directly in the desired path output_shapefile.

Return type

None