How to use the geojson.GeoJSON.to_instance function in geojson

To help you get started, we’ve selected a few geojson examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jazzband / geojson / tests / test_base.py View on Github external
def test_to_instance(self):
        FAKE = 'fake'
        self.assertEqual(FAKE, geojson.GeoJSON.to_instance(
            None, (lambda: FAKE)))

        with self.assertRaises(ValueError):
            geojson.GeoJSON.to_instance({"type": "Not GeoJSON"}, strict=True)
github hotosm / tasking-manager / osmtm / tests.py View on Github external
from sqlalchemy import create_engine
    engine = create_engine('postgresql://www-data@localhost/osmtm_tests')
    DBSession.configure(bind=engine)
    translation_manager.options.update({
        'locales': ['en'],
        'get_locale_fallback': True
    })
    configure_mappers()
    Base.metadata.drop_all(engine)
    Base.metadata.create_all(engine)

    with transaction.manager:
        import shapely
        import geojson
        geometry = '{"type":"Polygon","coordinates":[[[7.237243652343749,41.25922682850892],[7.23175048828125,41.12074559016745],[7.415771484374999,41.20552261955812],[7.237243652343749,41.25922682850892]]]}'
        geometry = geojson.loads(geometry, object_hook=geojson.GeoJSON.to_instance)
        geometry = shapely.geometry.asShape(geometry)
        geometry = shape.from_shape(geometry, 4326)

        area = Area(geometry)

        project = Project(
            u'Short project description',
        )
        project.area = area
        DBSession.add(project)
        project.auto_fill(12)
github descarteslabs / descarteslabs-python / descarteslabs / common / shapely_support / __init__.py View on Github external
def _parse_geojson_safe(geojson_dict):
    """
    Turns a dictionary into a GeoJSON instance in a safe way across different versions
    of the geojson library, without losing precision. Version 2.5.0 introduced a
    default FP precision of 6 for geometry coordinates, but we never want to lose
    precision. The maintainers have said they will remove the default precision again
    (https://github.com/jazzband/geojson/issues/135) but in the meantime we need to
    handle 2.5.0 in the wild.
    """
    try:
        geojson_dict = dict(geojson_dict)
        geojson_dict["precision"] = 40
        geoj = geojson.GeoJSON.to_instance(geojson_dict, strict=True)
    except (TypeError, KeyError, UnicodeEncodeError) as ex:
        raise ValueError(
            "geometry not recognized as valid GeoJSON ({}): {}".format(
                str(ex), geojson_dict
            )
        )

    # Prior to 2.5.0 this will exist as an attribute now, after 2.5.0 it won't
    if hasattr(geoj, "precision"):
        del geoj.precision

    return geoj
github hotosm / tasking-manager / server / services / grid / grid_service.py View on Github external
def _to_shapely_geometries(input: str) -> list:
        """
        Parses the input geojson and returns a list of geojson.Feature objects with their geometries
        adapted to shapely geometries
        :param input: string of geojson
        :return: list of geojson.Feature objects with their geometries adapted to shapely geometries
        """
        collection = geojson.loads(input, object_hook=geojson.GeoJSON.to_instance)

        if not hasattr(collection, "features") or len(collection.features) < 1:
            raise InvalidGeoJson("Geojson does not contain any features")

        shapely_features = list(
            (
                filter(
                    lambda x: x is not None,
                    map(GridService._adapt_feature_geometry, collection.features),
                )
            )
        )

        return shapely_features
github girder / girder / plugins / geospatial / server / geospatial.py View on Github external
def intersects(self, field, geometry, limit, offset, sort):
        try:
            GeoJSON.to_instance(geometry, strict=True)
        except (TypeError, ValueError):
            raise RestException("Invalid GeoJSON passed as 'geometry' parameter.")

        if field[:3] != '%s.' % GEOSPATIAL_FIELD:
            field = '%s.%s' % (GEOSPATIAL_FIELD, field)

        query = {
            field: {
                '$geoIntersects': {
                    '$geometry': geometry
                }
            }
        }

        return self._find(query, limit, offset, sort)
github hotosm / tasking-manager / osmtm / utils.py View on Github external
def parse_geojson(input):
    collection = geojson.loads(input,
                               object_hook=geojson.GeoJSON.to_instance)

    if not hasattr(collection, "features") or \
            len(collection.features) < 1:
        raise ValueError("GeoJSON file doesn't contain any feature.")
# need translation

    shapely_features = filter(lambda x: x is not None,
                              map(parse_feature, collection.features))

    if len(shapely_features) == 0:
        raise ValueError("GeoJSON file doesn't contain any polygon nor " +
                         "multipolygon.")
# need translation

    return shapely_features
github girder / girder / plugins / geospatial / server / geospatial.py View on Github external
def within(self, field, geometry, center, radius, limit, offset, sort):
        if geometry is not None:
            try:
                GeoJSON.to_instance(geometry, strict=True)

                if geometry['type'] != 'Polygon':
                    raise ValueError
            except (TypeError, ValueError):
                raise RestException("Invalid GeoJSON passed as 'geometry' parameter.")

            condition = {
                '$geometry': geometry
            }

        elif center is not None and radius is not None:
            try:
                radius /= self._RADIUS_OF_EARTH

                if radius < 0.0:
                    raise ValueError