fastpynuts.fastpynuts

This submodule defines the core functionality of fastpynuts.

Module Contents

Classes

NUTSregion

Hold a NUTS region’s geometry and bounding box for efficient querying. Properties from the NUTS dataset are accessible via the properties attribute.

NUTSfinder

Find NUTS regions for a point coordinate (lon, lat). Optionally restrict the NUTS levels of interest. Custom input files must follow the official NUTS naming convention.

API

class fastpynuts.fastpynuts.NUTSregion(feature, buffer=None)

Hold a NUTS region’s geometry and bounding box for efficient querying. Properties from the NUTS dataset are accessible via the properties attribute.

Initialization

Construct a NUTSregion from a geojson-like feature like

feature = {
    'type': 'Feature',
    'geometry': {
        'type': 'MultiPolygon',
        'coordinates': [[[[x1, y1], ..., [xN, yN]]]]
    },
    'properties': {"NUTS_ID": "DE", ...}
}
property id: str

The region’s ID as specified by the field NUTS_ID. E.g. ‘DE’

property level: int

The region’s level as specified by the field LEVL_CODE. E.g. 0

property type: str

The region’s feature type as specified by the geometry. Either 'Polygon' or 'MultiPolygon'.

class fastpynuts.fastpynuts.NUTSfinder(geojsonfile, buffer_geoms=None, min_level=0, max_level=3)

Find NUTS regions for a point coordinate (lon, lat). Optionally restrict the NUTS levels of interest. Custom input files must follow the official NUTS naming convention.

Note: Points-in-polyon tests via shapely may suffer from floating-point precision issues for points on the boundary of regions. A buffer to the regions may be introduced via the buffer_geoms keyword to ensure the correct assignment. On the flip-side, a buffer may lead to the assignment of multiple regions for points on the boundary.

Initialization

classmethod from_web(scale=1, year=2021, epsg=4326, level=None, datadir='.data', force_reload=False, temporary=False, **kwargs)

Download a NUTS file from Eurostat and construct a NUTSfinder object from it. If previously downloaded, use existing file instead. By default, the file will be saved in .data. The download location can be changed via the datadir keyword. The construction of the finder object can be specified via kwargs. For available keyword arguments, see the documentation of NUTSfinder.

to_geometry_collection()

Construct a shapely GeometryCollection from the finder’s regions.

to_geojson(geojsonfile)

Write the NUTSfinder’s regions to file as a FeatureCollection. Useful if the input dataset has been altered, i.e. by buffering, filtering, etc.

find(lon, lat, valid_point=False, **kwargs) list

Find a point’s NUTS regions by longitude and latitude. For large-scale applications, if it is known, that the point corresponds to a valid location within the NUTS regions, use valid_point = True for a speedup.

find_geometry(geom)

Find NUTS regions overlapping with a geometry.

geom must be either a shapely geometry that supports shapely.intersects or must be a GeoJSON-like geometry that can be converted into such a shapely geometry. See utils.geometry2shapely for info on supported formats of geom.

find_bbox(lon_min, lat_min, lon_max, lat_max)

Find NUTS regions overlapping with a rectangle (format (lon_min, lat_min, lon_max, lat_max)).

static filter_levels(regions, *levels)

Filter the results of a find operation for specific NUTS levels.

Usage

regions = nf.find_bbox((13.1, 47.2, 14.2, 49.0))

level3 = nf.filter_levels(regions, 3)
level2or3 = nf.filter_levels(regions, 2, 3)