Ejemplo n.º 1
0
def dumps(obj, srid=None):
    """
    Dump a GeoJSON-like `dict` to a Esri JSON.

    :param string:
        The GeoJSON geometry representation
    :param int:
        The default SRID value if none is present.

    """
    if 'type' in obj and \
            obj['type'].lower() in _gj_to_esri.keys():
        convert = _gj_to_esri[obj['type'].lower()]
        return convert(obj, srid=srid)
    else:
        raise geomet.InvalidGeoJSONException("Invalid GeoJSON type %s" % obj)
Ejemplo n.º 2
0
def dumps(obj, decimals=16):
    """
    Dump a GeoJSON-like `dict` to a WKT string.
    """
    try:
        geom_type = obj['type']
        exporter = _dumps_registry.get(geom_type)

        if exporter is None:
            _unsupported_geom_type(geom_type)

        # Check for empty cases
        if geom_type == 'GeometryCollection':
            if len(obj['geometries']) == 0:
                return 'GEOMETRYCOLLECTION EMPTY'
        else:
            # Geom has no coordinate values at all, and must be empty.
            if len(list(util.flatten_multi_dim(obj['coordinates']))) == 0:
                return '%s EMPTY' % geom_type.upper()
    except KeyError:
        raise geomet.InvalidGeoJSONException('Invalid GeoJSON: %s' % obj)

    result = exporter(obj, decimals)
    # Try to get the SRID from `meta.srid`
    meta_srid = obj.get('meta', {}).get('srid')
    # Also try to get it from `crs.properties.name`:
    crs_srid = obj.get('crs', {}).get('properties', {}).get('name')
    if crs_srid is not None:
        # Shave off the EPSG prefix to give us the SRID:
        crs_srid = crs_srid.replace('EPSG', '')

    if (meta_srid is not None and crs_srid is not None
            and str(meta_srid) != str(crs_srid)):
        raise ValueError('Ambiguous CRS/SRID values: %s and %s' %
                         (meta_srid, crs_srid))
    srid = meta_srid or crs_srid

    # TODO: add tests for CRS input
    if srid is not None:
        # Prepend the SRID
        result = 'SRID=%s;%s' % (srid, result)
    return result
Ejemplo n.º 3
0
def loads(string):
    """
    Construct a GeoJSON `dict` from Esri JSON (string/dict).

    :param string:
        The Esri JSON geometry representation

    :returns:
         A GeoJSON `dict` representing the geometry read from the file.
    """
    data = json.loads(string)

    if 'rings' in data:
        return _esri_to_geojson_convert['rings'](data)
    elif 'paths' in data:
        return _esri_to_geojson_convert['paths'](data)
    elif 'x' in data or 'y' in data:
        return _esri_to_geojson_convert['x'](data)
    elif 'points' in data:
        return _esri_to_geojson_convert['points'](data)
    else:
        raise geomet.InvalidGeoJSONException('Invalid EsriJSON: %s' % string)
Ejemplo n.º 4
0
def dumps(obj, decimals=16):
    """
    Dump a GeoJSON-like `dict` to a WKT string.
    """
    try:
        geom_type = obj['type']
        exporter = _dumps_registry.get(geom_type)

        if exporter is None:
            _unsupported_geom_type(geom_type)

        # Check for empty cases
        if geom_type == 'GeometryCollection':
            if len(obj['geometries']) == 0:
                return 'GEOMETRYCOLLECTION EMPTY'
        else:
            # Geom has no coordinate values at all, and must be empty.
            if len(list(util.flatten_multi_dim(obj['coordinates']))) == 0:
                return '%s EMPTY' % geom_type.upper()
    except KeyError:
        raise geomet.InvalidGeoJSONException('Invalid GeoJSON: %s' % obj)

    fmt = '%%.%df' % decimals
    return exporter(obj, fmt)