pyprecag.convert

Format conversion routines.

pyprecag.convert.add_point_geometry_to_dataframe(in_dataframe, coord_columns=None, coord_columns_epsg=0, out_epsg=0)[source]
Add Point geometry to a pandas or geopandas data frame through sets of coordinate columns.
If required, it will also reproject to the chosen EPSG coordinate system
Parameters:
  • in_dataframe (DataFrame) – The input dataframe. This could be a pandas or geopandas dataframe
  • coord_columns (list) – A list of the columns representing coordinates. If None, it will be predicted.
  • coord_columns_epsg (int) – The EPSG number representing the coordinates inside the csv file
  • out_epsg (int) – The EPSG number representing the required output coordinates system. Use 0 for no projection
Returns:

A geodataframe of Points with ALL attributes.

Return type:

(GeoDataFrame)

Modified from: https://gis.stackexchange.com/a/182234

pyprecag.convert.convert_csv_to_points(in_csvfilename, out_shapefilename=None, coord_columns=None, coord_columns_epsg=4326, out_epsg=0)[source]

Converts a comma or tab delimited file to a shapefile.

If EPSG is omitted it will default to 0 or unknown.

Parameters:
  • in_csvfilename (str) – Input CSV or Txt (tab) file.
  • out_shapefilename (str) – Output Shapefile Name. If path is excluded it uses TEMPDIR
  • coord_columns (list) – Column list representing coordinates. If None, it will be predicted.
  • coord_columns_epsg (int) – EPSG number for specified coord_columns. Default is 4326 (WGS84)
  • out_epsg (int) – The EPSG number representing the required output coordinates system. - OR - 0 is no reprojection -1 calculate utm zonal projection
Returns:

A geodataframe of Points with ALL attributes. (pyprecag_crs): The coordinate system details of the dataframe

Return type:

(GeoDataFrame)

Modified from: https://gis.stackexchange.com/a/182234

pyprecag.convert.convert_grid_to_vesper(in_rasterfilename, out_vesperfilename)[source]

Convert a Grid the format compatible with vesper.

The vesper format contains the xy coordinates for the center of each pixel with one space before the x and three spaces between the x and y values.

The coordinates are written as rows from the Upper Left corner of the raster.

Parameters:
  • in_rasterfilename (str) – An input raster file
  • out_vesperfilename (str) – Save the output vesper file to this file.
pyprecag.convert.convert_polygon_feature_to_raster(feature, pixel_size, value=1, nodata_val=-9999, snap_extent_to_pixel=True)[source]

Convert a feature into a raster (block grid)

The “value” argument can be a numeric, or the name of a feature column. If the feature column is a string, then the index value will be used to assign a value to the raster.

By default, snap_extent_to_pixel is true and is used to snap the bounding coordinates to a pixel edge.

Parameters:
  • feature (pandas.core.series.Series) – a polygon feature to convert to a raster
  • (int, float] (pixel_size) – the pixel size for the raster
  • value (int, float, str) – pixel value. A column from the feature can be used
  • nodata_val (int, float) – nodata value
  • snap_extent_to_pixel (bool) – round the extent coordinates to be divisible by the pixel size
Returns:

The array representing the raster data Dict[str, int]] : The rasterio meta kwargs required for writing the array to disk.

Return type:

burned (numpy.ndarray)

pyprecag.convert.convert_polygon_to_grid(in_shapefilename, out_rasterfilename, pixel_size, nodata_val=-9999, snap_extent_to_pixel=True, overwrite=True)[source]

Convert a polygon shapefile to a raster for a set pixel size.

source : http://ceholden.github.io/open-geo-tutorial/python/chapter_4_vector.html

Parameters:
  • in_shapefilename (str) – A polygon shapefile to rasterise
  • out_rasterfilename (str) – the output raster name
  • pixel_size (float) – the pixel size for the raster
  • nodata_val (int) – the value to use for nodata
  • snap_extent_to_pixel (bool) – round the extent coordinates to be divisible by the pixel size
  • overwrite (bool) – if true overwrite existing output file
Returns:

Return type:

None

pyprecag.convert.deg_to_16_compass_pts(degrees)[source]

Convert a compass bearing from north using 16 cardinal directions :param degrees: Degrees from north :type degrees: float

Returns:letters representing the compass point
Return type:direction(str)

source: https://stackoverflow.com/a/7490772

pyprecag.convert.deg_to_8_compass_pts(degrees)[source]

Convert a compass bearing from north using 8 cardinal directions :param degrees: Degrees from north :type degrees: float

Returns:letters representing the compass point
Return type:direction(str)

source: variation of https://stackoverflow.com/a/7490772

pyprecag.convert.drop_z(geom)[source]
pyprecag.convert.line_bearing(line)[source]

The bearing on the line between the start and end nodes/points

pyprecag.convert.numeric_pixelsize_to_string(pixelsize_metres)[source]

Convert a numeric pixel size to a string with the appropriate units for use in file names. For example 0.40 is 40cm, 0.125 is 125mm, 1.5km is 1500m, 2.0 is 2m, 6000 is 6km :param pixelsize_metres: The pixel size in meters. :type pixelsize_metres: numeric

Returns (str): a string representation of the pixel size

pyprecag.convert.point_to_point_bearing(pt1, pt2)[source]

Calculate bearing from north between two points

pyprecag.convert.text_rotation(pt1, pt2)[source]