Пример #1
0
def test_convert_geobuf(landpoly, mp_tmpdir):
    # convert to geobuf
    geobuf_outdir = os.path.join(mp_tmpdir, "geobuf")
    run_cli([
        "convert", landpoly, geobuf_outdir, "--output-pyramid", "geodetic",
        "--zoom", "4", "--output-format", "Geobuf"
    ])
    for (zoom, row, col), control in zip([(4, 0, 7), (4, 1, 7)], [9, 32]):
        out_file = os.path.join(
            *[geobuf_outdir,
              str(zoom), str(row),
              str(col) + ".pbf"])
        with open(out_file, "rb") as src:
            features = geobuf.decode(src.read())["features"]
            assert len(features) == control
            for f in features:
                assert f["geometry"]["type"] == "Polygon"
                assert shape(f["geometry"]).area

    # convert from geobuf
    geojson_outdir = os.path.join(mp_tmpdir, "geojson")
    run_cli([
        "convert", geobuf_outdir, geojson_outdir, "--zoom", "4",
        "--output-format", "GeoJSON"
    ])
    for (zoom, row, col), control in zip([(4, 0, 7), (4, 1, 7)], [9, 32]):
        out_file = os.path.join(
            *[geojson_outdir,
              str(zoom),
              str(row),
              str(col) + ".geojson"])
        with fiona.open(out_file, "r") as src:
            assert len(src) == control
            for f in src:
                assert shape(f["geometry"]).is_valid
Пример #2
0
def reading(filepath):
    #import os
    #print os.getcwd(), filepath
    with gzip.open(filepath, 'rb') as f:
        pbf = f.read()

    my_json = geobuf.decode(pbf)
    gdf = gpd.GeoDataFrame.from_features(my_json['features'])
    #print gdf.head()
    return gdf
Пример #3
0
 def test_api_can_decompress_geojson(self):
     """
     Ensure we send geometry as a valid geobuf.
     """
     url = reverse("territory-list")
     response = self.client.get(url, format="json")
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     gbuf = bytes.fromhex(response.data[0]["geo"])
     geojson = json.dumps(geobuf.decode(gbuf))
     self.assertEqual(
         geojson,
         '{"type": "MultiPolygon", "coordinates": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]], [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]]}',
     )
Пример #4
0
def decode():
    """Given a Geobuf byte string on stdin, write a GeoJSON feature
    collection to stdout."""
    logger = logging.getLogger('geobuf')
    stdin = click.get_binary_stream('stdin')
    sink = click.get_text_stream('stdout')
    try:
        pbf = stdin.read()
        data = geobuf.decode(pbf)
        json.dump(data, sink)
        sys.exit(0)
    except Exception:
        logger.exception("Failed. Exception caught")
        sys.exit(1)
Пример #5
0
def test_convert_geobuf(landpoly, mp_tmpdir):
    # convert to geobuf
    geobuf_outdir = os.path.join(mp_tmpdir, "geobuf")
    run_cli([
        "convert", landpoly, geobuf_outdir, "--output-pyramid", "geodetic",
        "--zoom", "4", "--output-format", "Geobuf"
    ])
    for (zoom, row, col), control in zip([(4, 0, 7), (4, 1, 7)], [9, 32]):
        out_file = os.path.join(
            *[geobuf_outdir,
              str(zoom), str(row),
              str(col) + ".pbf"])
        with open(out_file, "rb") as src:
            features = geobuf.decode(src.read())["features"]
            assert len(features) == control
            for f in features:
                assert f["geometry"]["type"] == "Polygon"
                assert shape(f["geometry"]).area

    # convert from geobuf
    # NOTE: if shapely was built using GEOS 3.8.0 or smaller, there is one more feature
    if version_is_greater_equal(shapely.geos.geos_version, (3, 9, 0)):
        zoom9_control = 31
    else:
        zoom9_control = 32

    geojson_outdir = os.path.join(mp_tmpdir, "geojson")
    run_cli([
        "convert", geobuf_outdir, geojson_outdir, "--zoom", "4",
        "--output-format", "GeoJSON"
    ])
    for (zoom, row, col), control in zip([(4, 0, 7), (4, 1, 7)],
                                         [9, zoom9_control]):
        out_file = os.path.join(
            *[geojson_outdir,
              str(zoom),
              str(row),
              str(col) + ".geojson"])
        with fiona.open(out_file, "r") as src:
            assert len(src) == control
            for f in src:
                assert shape(f["geometry"]).is_valid
Пример #6
0
def test_convert_geobuf_multipolygon(landpoly, mp_tmpdir):
    run_cli([
        "convert", landpoly, mp_tmpdir, "--output-pyramid", "geodetic",
        "--zoom", "4", "--output-format", "Geobuf", "--output-geometry-type",
        "MultiPolygon"
    ])
    for (zoom, row, col), control in zip([(4, 0, 7), (4, 1, 7)], [7, 30]):
        out_file = os.path.join(
            *[mp_tmpdir, str(zoom),
              str(row), str(col) + ".pbf"])
        with open(out_file, "rb") as src:
            features = geobuf.decode(src.read())["features"]
            assert len(features) == control
            multipolygons = 0
            for f in features:
                assert f["geometry"]["type"] in ["Polygon", "MultiPolygon"]
                assert shape(f["geometry"]).area
                if f["geometry"]["type"] == "MultiPolygon":
                    multipolygons += 1
            assert multipolygons
Пример #7
0
    def read(self, output_tile, **kwargs):
        """
        Read existing process output.

        Parameters
        ----------
        output_tile : ``BufferedTile``
            must be member of output ``TilePyramid``

        Returns
        -------
        process output : list
        """
        import geobuf
        path = self.get_path(output_tile)
        try:
            with fs_from_path(path).open(path, "rb") as src:
                return geobuf.decode(src.read()).get("features", [])
        except FileNotFoundError:
            return self.empty(output_tile)
Пример #8
0
def test_for_web(client, mp_tmpdir):
    """Send Geobuf via flask."""
    tile_base_url = '/wmts_simple/1.0.0/geobuf/default/WGS84/'
    for url in ["/"]:
        response = client.get(url)
        assert response.status_code == 200
    features = 0
    for url in [
            tile_base_url + "4/12/31.pbf",
            tile_base_url + "4/12/30.pbf",
            tile_base_url + "4/11/31.pbf",
            tile_base_url + "4/11/30.pbf",
    ]:
        response = client.get(url)
        assert response.status_code == 200
        assert response.mimetype == "application/octet-stream"
        pbf = gb.decode(response.data)
        for feature in pbf["features"]:
            print(shape(feature["geometry"]))
            assert shape(feature["geometry"]).buffer(0).is_valid
            features += 1
    assert features
Пример #9
0
def decode_geometry(geom: str) -> BasePolygon:
    """
    Decode geometry from compressed string
    """
    return shape(geobuf.decode(bytes.fromhex(geom))).buffer(0)
Пример #10
0
 def deserialize(self, data: bytes) -> dict:
     return geobuf.decode(data)
Пример #11
0
def main():
    buf = encode(GEO)
    data = Data()
    data.ParseFromString(buf)
    print(data)
    print(decode(buf))