geofileops.apply#

geofileops.apply(input_path: Union[str, os.PathLike[Any]], output_path: Union[str, os.PathLike[Any]], func: Callable[[Any], Any], only_geom_input: bool = True, input_layer: Optional[str] = None, output_layer: Optional[str] = None, columns: Optional[List[str]] = None, explodecollections: bool = False, force_output_geometrytype: Optional[Union[GeometryType, str]] = None, gridsize: float = 0.0, keep_empty_geoms: Optional[bool] = None, where_post: Optional[str] = None, nb_parallel: int = -1, batchsize: int = -1, force: bool = False)#

Apply a python lambda function on the geometry column of the input file.

The result is written to the output file specified.

If explodecollections is False and the input and output file type is GeoPackage, the fid will be preserved. In other cases this will typically not be the case.

Parameters:
  • input_path (PathLike) – the input file

  • output_path (PathLike) – the file to write the result to

  • func (Callable) – lambda function to apply to the geometry column.

  • only_geom_input (bool, optional) – If True, only the geometry column is available. If False, the entire row is input. Remark: when False, the operation is 50% slower. Defaults to True.

  • input_layer (str, optional) – input layer name. Optional if the input file only contains one layer.

  • output_layer (str, optional) – input layer name. Optional if the input file only contains one layer.

  • columns (List[str], optional) – list of columns to retain. If None, all standard columns are retained. In addition to standard columns, it is also possible to specify “fid”, a unique index available in all input files. Note that the “fid” will be aliased eg. to “fid_1”. Defaults to None.

  • explodecollections (bool, optional) – True to output only simple geometries. Defaults to False.

  • force_output_geometrytype (GeometryType, optional) – The output geometry type to force. If None, a best-effort guess is made and will always result in a multi-type. Defaults to None.

  • gridsize (float, optional) – the size of the grid the coordinates of the ouput will be rounded to. Eg. 0.001 to keep 3 decimals. Value 0.0 doesn’t change the precision. Defaults to 0.0.

  • keep_empty_geoms (bool, optional) – True to keep rows with empty/null geometries in the output. Defaults to False.

  • where_post (str, optional) – SQL filter to apply after all other processing, including e.g. explodecollections. It should be in sqlite syntax and spatialite reference functions can be used. Defaults to None.

  • nb_parallel (int, optional) – the number of parallel processes to use. Defaults to -1: use all available CPUs.

  • batchsize (int, optional) – indicative number of rows to process per batch. A smaller batch size, possibly in combination with a smaller nb_parallel, will reduce the memory usage. Defaults to -1: (try to) determine optimal size automatically.

  • force (bool, optional) – overwrite existing output file(s). Defaults to False.

Examples

This example shows the basic usage of gfo.apply:

import geofileops as gfo

gfo.apply(
    input_path=...,
    output_path=...,
    func=lambda geom: pygeoops.remove_inner_rings(geom, min_area_to_keep=1),
)

If you need to use the contents of other columns in your lambda function, you can call gfo.apply like this:

import geofileops as gfo

gfo.apply(
    input_path=...,
    output_path=...,
    func=lambda row: pygeoops.remove_inner_rings(
        row.geometry, min_area_to_keep=row.min_area_to_keep
    ),
    only_geom_input=False,
)