GeoModeler is a Python project based on pydantic, designed to model and validate geojson data structures such as points, lines, and polygons. It provides a set of tools and validators for working with GeoJSON data.
Project description
GeoJSON Models
This project provides a set of Python models for GeoJSON objects conforming to RFC 7946. It includes models for all the basic GeoJSON types:
- Point
- MultiPoint
- LineString
- MultiLineString
- Polygon
- MultiPolygon
- GeometryCollection
- Feature
- FeatureCollection
These models are implemented using the Pydantic library, which provides runtime data validation and settings management using Python type annotations.
Installation
This project requires Python 3.10 or higher. You can install it using pip:
pip install geo-modeler
Usage
Here is an example of how to create a Point object:
from src import Point
point = Point(type='Point', coordinates=[1.0, 2.0])
You can also validate a GeoJSON string:
point = Point.validate_json('{"type":"Point","coordinates":[1.0,2.0]}')
And convert a model to a GeoJSON string:
json_string = point.model_dump_json()
Initializing and Validating
To initialize a FeatureCollection
or a Point
using their corresponding JSON representations, you can use the validate_json
method provided by the geojson_models
library. This method will take care of all subtypes and validate the structure of the GeoJSON.
If the GeoJSON does not follow the right-hand rule, a warning will be issued, but it won't break the execution.
When dumping the model to a GeoJSON string using the model_dump_json
method, you can use the unset
option to exclude empty defaults.
Here is an example of how to use these features:
from src import FeatureCollection, Point
# Initialize a FeatureCollection with data Point from a JSON string.
feature_collection_json = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":"1","name":"Litter Bin","description":"Litter Bin","type":"Litter Bin","colour":"Green","location":"Leeds","location_type":"Street","location_subtype":"Road","location_name":"Leeds","latitude":"53.71583","longitude":"-1.74448","easting":"429000","northing":"433000","northing":"433000","postcode_sector":"LS1","postcode_district":"LS","postcode_area":"LS","uprn":"100335","organisation":"Leeds City Council","organisation_uri":"http://opendatacommunities.org/id/leeds-city-council","organisation_label":"Leeds City Council","uri":"http://opendatacommunities.org/id/litter-bin/leeds/1","label":"Litter Bin","notation":"1","notation_uri":"http://opendatacommunities.org/id/litter-bin/leeds/1","notation_label":"1","notation_type":"http://opendatacommunities.org/def/litter-bin/leeds/notation","notation_type_label":"Notation"},"geometry":{"type":"Point","coordinates":[-1.74448,53.71583]}}]}'
feature_collection = FeatureCollection.model_validate_json(feature_collection_json)
# Initialize a single Point with a JSON string
point_json = '{"type":"Point","coordinates":[1.0,2.0]}'
point = Point.model_validate_json(point_json)
# Dump the models to GeoJSON strings, excluding empty defaults
print(feature_collection.model_dump_json(exclude_unset=True))
# {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"id":"1","name":"Litter Bin","description":"Litter Bin","type":"Litter Bin","colour":"Green","location":"Leeds","location_type":"Street","location_subtype":"Road","location_name":"Leeds","latitude":"53.71583","longitude":"-1.74448","easting":"429000","northing":"433000","postcode_sector":"LS1","postcode_district":"LS","postcode_area":"LS","uprn":"100335","organisation":"Leeds City Council","organisation_uri":"http://opendatacommunities.org/id/leeds-city-council","organisation_label":"Leeds City Council","uri":"http://opendatacommunities.org/id/litter-bin/leeds/1","label":"Litter Bin","notation":"1","notation_uri":"http://opendatacommunities.org/id/litter-bin/leeds/1","notation_label":"1","notation_type":"http://opendatacommunities.org/def/litter-bin/leeds/notation","notation_type_label":"Notation"},"geometry":{"type":"Point","coordinates":[-1.74448,53.71583]}}]}
print(point.model_dump_json())
# {"type":"Point","coordinates":[1.0,2.0]}
This will output valid GeoJSON, although it may contain warnings for direction. The model_validate_json
method also validates lengths and required fields according to the GeoJSON specification.
Testing
This project includes a suite of tests that you can run using pytest:
pytest
Contributing
Contributions are welcome! Please feel free to submit a pull request.
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for geomodeler-0.1.0.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 27b15a8f8e83f4ec4153a9631a69226ae2be8a29aaddfe7a9c263ad43621e7fc |
|
MD5 | 383158a14467291372c96b622aef0b81 |
|
BLAKE2b-256 | a7f3d13f4ba6949f6243ef88faf29321ffc8c3cae770596de0a03603497914b5 |
Hashes for GeoModeler-0.1.dev0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d4f5cb0caf7830f72a1b5a91e522f49c90fffb557814f92528b06af1ed9853f |
|
MD5 | e8e03f6e3c3aaba43f114fff4da05d6b |
|
BLAKE2b-256 | bd983720d4fb60e28fd874108993ae0bc4977fb7885c537626bc1348d2f47958 |