Top

pygeoj module

API Documentation

Functions

def load(

filepath=None, data=None, **kwargs)

Loads a geojson file or dictionary, validates it, and returns a GeojsonFile instance.

In order for a geojson dict to be considered a file, it cannot just be single geometries, so this class always saves them as the toplevel FeatureCollection type, and requires the files it loads to be the same.

To load with a different text encoding use the 'encoding' argument.

Parameters:

  • filepath (optional): The path of a geojson file to load.
  • data (optional): A complete geojson dictionary to load.

Returns:

  • A GeojsonFile instance.

def new(

)

Creates a new empty geojson file instance.

Returns:

  • An empty GeojsonFile instance.

def validate(

data, skiperrors=False, fixerrors=True)

Checks that the geojson data is a feature collection, that it contains a proper "features" attribute, and that all features are valid too. Returns True if all goes well.

  • skiperrors will throw away any features that fail to validate.
  • fixerrors will attempt to auto fix any minor errors without raising exceptions.

Classes

class Feature

A feature instance, as an object representation of GeoJSON's feature dictinoary item, with some convenience methods.

Attributes:

  • geometry: A Geometry instance.
  • properties: A properties dictionary

Ancestors (in MRO)

Methods

def __init__(

self, obj=None, geometry=None, properties=None)

If obj isn't specified, geometry and properties can be set as arguments directly.

Parameters:

  • obj: Another feature instance, an object with the __geo_interface__ or a geojson dictionary of the Feature type.
  • geometry (optional): Anything that the Geometry instance can accept.
  • properties (optional): A dictionary of key-value property pairs.

def validate(

self, fixerrors=True)

Validates that the feature is correctly formatted.

Parameters:

  • fixerrors (optional): Attempts to fix minor errors without raising exceptions (defaults to True)

Returns:

  • True if the feature is valid.

Raises:

  • An Exception if not valid.

class GeojsonFile

An instance of a geojson file.

Attributes:

  • crs: The geojson formatted dictionary of the file's coordinate reference system. Read only. Call .define_crs() to change it.
  • bbox: The bounding box surrounding all geometries in the file. Read only. You may need to call .update_bbox() to make sure this one is up-to-date.
  • all_attributes: Collect and return a list of all attributes/properties/fields used in any of the features. Read only.
  • common_attributes: Collects and returns a list of attributes/properties/fields common to all features. Read only.

Ancestors (in MRO)

Methods

def __init__(

self, filepath=None, data=None, skiperrors=False, fixerrors=True, **kwargs)

Can load from data or from a file, which can then be read or edited. Call without any arguments to create an empty geojson file so you can construct it from scratch.

In order for a geojson dict to be considered a file, it cannot just be single geometries, so this class always saves them as the toplevel FeatureCollection type, and requires the files it loads to be the same.

Parameters:

  • filepath (optional): The path of a geojson file to load.
  • data (optional): A complete geojson dictionary to load.
  • skiperrors (optional): Throws away any features that fail to validate (defaults to False).
  • fixerrors (optional): Attempts to auto fix any minor errors without raising exceptions (defaults to True).

def add_all_bboxes(

self)

Calculates and adds a bbox attribute to the geojson entry of all feature geometries, updating any existing ones.

def add_feature(

self, obj=None, geometry=None, properties=None)

Adds a given feature. If obj isn't specified, geometry and properties can be set as arguments directly.

Parameters:

  • obj: Another feature instance, an object with the __geo_interface__ or a geojson dictionary of the Feature type.
  • geometry (optional): Anything that the Geometry instance can accept.
  • properties (optional): A dictionary of key-value property pairs.

def add_unique_id(

self)

Adds a unique id property to each feature.

Raises:

  • An Exception if any of the features already have an "id" field.

def addfeature(

self, feature)

Backwards compatible legacy method. WARNING: Will be depreceated.

def define_crs(

self, type, name=None, link=None, link_type=None)

Defines the coordinate reference system for the geojson file. For link crs, only online urls are currenlty supported (no auxilliary crs files).

Parameters:

  • type: The type of crs, either "name" or "link".
  • name (optional): The crs name as an OGC formatted crs string (eg "urn:ogc:def:crs:..."), required if type is "name"
  • link: The crs online url link address, required if type is "link".
  • link_type: The type of crs link, optional if type is "link".

def get_feature(

self, index)

Gets a feature based on its index. Same as feat[index].

Parameters:

  • index: The index position of the feature to get.

Returns:

  • A Feature instance.

def getfeature(

self, index)

Backwards compatible legacy method. WARNING: Will be depreceated.

def remove_feature(

oldindex)

Removes a feature at a specified index position.

Parameters:

  • oldindex: The index position of the feature to be removed.

def removefeature(

oldindex)

Backwards compatible legacy method. WARNING: Will be depreceated.

def replace_feature(

self, oldindex, newfeature)

Replaces the feature at an index position with another.

Parameters:

  • oldindex: The index position of the feature to be replaced.
  • newfeature: Anything that the Feature instance can accept.

def replacefeature(

self, oldindex, newfeature)

Backwards compatible legacy method. WARNING: Will be depreceated.

def save(

self, savepath, **kwargs)

Saves the geojson instance to file. To save with a different text encoding use the 'encoding' argument.

Parameters:

  • savepath: Filepath to save the file.

def update_bbox(

self)

Recalculates the bbox region attribute for the entire file. Useful after adding and/or removing features.

No need to use this method just for saving, because saving automatically updates the bbox.

class Geometry

A geometry instance, as an object representation of GeoJSON's geometry dictinoary item, with some convenience methods.

Attributes:

  • type: As specified when constructed
  • coordinates: As specified when constructed
  • bbox: If the bounding box wasn't specified when constructed then it is calculated on-the-fly.

Ancestors (in MRO)

Methods

def __init__(

self, obj=None, type=None, coordinates=None, bbox=None)

Can be created from args, or without any to create an empty one from scratch. If obj isn't specified, type, coordinates, and optionally bbox can be set as arguments

Parameters:

  • obj: Another geometry instance, an object with the __geo_interface__ or a geojson dictionary of the Geometry type
  • type (optional): The type of geometry. Point, MultiPoint, LineString, MultiLineString, Polygon, or MultiPolygon.
  • coordinates (optional): A sequence of coordinates formatted according to the geometry type.
  • bbox (optional): The bounding box of the geometry as [xmin, ymin, xmax, ymax].

def update_bbox(

self)

Removes any existing stored bbox attribute from the geojson dictionary. This way, until you set the bbox attribute again, the bbox will always be calculated on the fly and be up to date. Useful after making changes to the geometry coordinates.

def validate(

self, fixerrors=True)

Validates that the geometry is correctly formatted according to the geometry type.

Parameters:

  • fixerrors (optional): Attempts to fix minor errors without raising exceptions (defaults to True)

Returns:

  • True if the geometry is valid.

Raises:

  • An Exception if not valid.