Ejemplo n.º 1
0
def test_valid(src, is_valid):
    if 'wkb' in src:
        src['wkb'] = bytes.fromhex(src['wkb'])
    if not is_valid:
        with pytest.raises(GeometryNotValid):
            Geometry(validate=True, **src)
    else:
        Geometry(validate=True, **src)
Ejemplo n.º 2
0
def test_wkt_wkb_ogr_shape():
    wkt = Geometry.from_wkt('POINT Z (1 2 3)')

    wkb = Geometry.from_wkb(wkt.wkb)
    assert wkt.wkt == wkb.wkt

    ogr_geom = Geometry.from_ogr(wkb.ogr)
    assert wkb.wkb == ogr_geom.wkb

    shape = Geometry.from_shape(ogr_geom.shape)
    assert shape.wkt == wkt.wkt
Ejemplo n.º 3
0
def test_geom_transform(ngw_webtest_app, srs_msk23_id):
    result = ngw_webtest_app.post_json(
        "/api/component/spatial_ref_sys/%d/geom_transform" % 3857,
        dict(geom=MOSCOW_VLADIVOSTOK, srs=4326))
    g1 = Geometry.from_wkt(result.json["geom"])
    g2 = Geometry.from_wkt(
        "LINESTRING(4187839.2436 7508807.8513,14683040.8356 5330254.9437)")
    assert g2.shape.equals_exact(g1.shape, 5e-05)

    result = ngw_webtest_app.post_json(
        "/api/component/spatial_ref_sys/%d/geom_transform" % 4326,
        dict(geom=result.json["geom"], srs=3857))
    g1 = Geometry.from_wkt(result.json["geom"])
    g2 = Geometry.from_wkt(MOSCOW_VLADIVOSTOK)
    assert g2.shape.equals_exact(g1.shape, 5e-07)
Ejemplo n.º 4
0
    def render_image(self, srs, extent, size, cond, padding=0):
        res_x = (extent[2] - extent[0]) / size[0]
        res_y = (extent[3] - extent[1]) / size[1]

        # Экстент с учетом отступов
        extended = (
            extent[0] - res_x * padding,
            extent[1] - res_y * padding,
            extent[2] + res_x * padding,
            extent[3] + res_y * padding,
        )

        # Размер изображения с учетом отступов
        render_size = (size[0] + 2 * padding, size[1] + 2 * padding)

        # Фрагмент изображения размера size
        target_box = (padding, padding, size[0] + padding, size[1] + padding)

        # Выбираем объекты по экстенту
        feature_query = self.parent.feature_query()

        # Отфильтровываем объекты по условию
        if cond is not None:
            feature_query.filter_by(**cond)

        feature_query.srs(srs)

        feature_query.intersects(Geometry.from_box(*extended, srid=srs.id))
        feature_query.geom()
        features = list(feature_query())

        if len(features) == 0:
            return None

        mapobj = self._mapobj(features)

        # Получаем картинку эмулируя WMS запрос
        req = mapscript.OWSRequest()
        req.setParameter("bbox",
                         ','.join(map(str, extended if padding else extent)))
        req.setParameter("width", str(render_size[0]))
        req.setParameter("height", str(render_size[1]))
        req.setParameter("srs", 'EPSG:%d' % self.parent.srs_id)
        req.setParameter("format", 'image/png')
        req.setParameter("layers", 'main')
        req.setParameter("request", "GetMap")
        req.setParameter('transparent', 'TRUE')

        mapobj.loadOWSParameters(req)
        gdimg = mapobj.draw()

        # Преобразуем изображение из PNG в объект PIL
        buf = BytesIO()
        buf.write(gdimg.getBytes())
        buf.seek(0)

        img = Image.open(buf)

        # Вырезаем нужный нам кусок изображения
        return img.crop(target_box)
Ejemplo n.º 5
0
def cmp_geom(gj_geom, geom2, srs):
    geom1 = Geometry.from_geojson(gj_geom)
    if srs.id != 4326:
        t = Transformer(WKT_EPSG_4326, srs.wkt)
        geom1 = t.transform(geom1)
    g1 = geom1.shape
    g2 = geom2.shape
    assert g1.equals_exact(g2, 5e-07)
Ejemplo n.º 6
0
def test_geom_area(wkt_geom, epsg_geom, epsg_calc, area, length):
    geom = Geometry.from_wkt(wkt_geom)
    crs_geom = CRS.from_epsg(epsg_geom).to_wkt()
    crs_calc = CRS.from_epsg(epsg_calc).to_wkt()
    geom = Transformer(crs_geom, crs_calc).transform(geom)

    assert geom_area(geom, crs_calc) == pytest.approx(area,
                                                      abs=abs(area) * 0.05)
    assert geom_length(geom, crs_calc) == pytest.approx(length,
                                                        abs=abs(length) * 0.05)
Ejemplo n.º 7
0
def test_geom():
    wkt = 'POINT(1 0)'

    geom = Geometry.from_wkt(wkt)
    assert geom.wkt == wkt

    shape = Point(1, 0)
    assert geom.shape.equals(shape)

    assert geom.wkb == shape.wkb

    assert geom.to_geojson() == dict(type='Point', coordinates=(1, 0))
Ejemplo n.º 8
0
def test_wkt_wkb(wkt, ngw_txn):
    ogr_geom = ogr.CreateGeometryFromWkt(wkt)
    assert ogr_geom is not None, gdal.GetLastErrorMsg()
    wkt_iso = ogr_geom.ExportToIsoWkt()

    shp_geom = wkt_loads(wkt)
    wkb_shp = shp_geom.wkb
    assert shp_geom.wkt == wkt_iso, "Shapely WKT didn't return ISO WKT"

    wkb_iso = _pg_wkt_to_wkb_iso(wkt)
    wkb_ext = _pg_wkt_to_wkb_ext(wkt)

    assert wkb_shp == wkb_ext, "PostGIS EWKT and Shapely WKB didn't match"
    assert wkb_loads(
        wkb_iso).wkt == wkt_iso, "Shapely didn't understand ISO WKB"
    assert wkb_loads(wkb_ext).wkt == wkt_iso, "Shapely didn't understand EWKB"

    assert _pg_wkb(wkb_iso) == wkb_ext, "PostGIS didn't understand ISO WKB"
    assert _pg_wkb(wkb_ext) == wkb_ext, "PostGIS didn't understand EWKB"

    assert Geometry.from_wkb(
        wkb_iso).wkt == wkt_iso, "ISO WKB parsing has failed"
    assert Geometry.from_wkb(wkb_ext).wkt == wkt_iso, "EWKB parsing has failed"
    assert Geometry.from_wkt(wkt_iso).wkb == wkb_ext, "WKT parsing has failed"
Ejemplo n.º 9
0
def test_invalidate(frtc, img_cross_red, img_cross_green, img_fill, caplog):
    caplog.set_level(logging.DEBUG)
    tile_invalid = (4, 0, 0)
    tile_valid = (4, 15, 15)

    frtc.put_tile(tile_invalid, img_cross_red)
    frtc.put_tile(tile_valid, img_cross_red)

    frtc.invalidate(
        Geometry.from_shape(
            Point(*frtc.resource.srs.tile_center(tile_invalid)), srid=None))

    exists, cimg = frtc.get_tile(tile_invalid)
    assert not exists

    exists, cimg = frtc.get_tile(tile_valid)
    assert exists and cimg.getextrema() == img_cross_red.getextrema()

    # Update previously invalidated tile
    frtc.put_tile(tile_invalid, img_cross_green)
    exists, cimg = frtc.get_tile(tile_invalid)
    assert exists and cimg.getextrema() == img_cross_green.getextrema()
Ejemplo n.º 10
0
 def wkt_compare(wkt1, wkt2):
     g1 = Geometry.from_wkt(wkt1)
     g2 = Geometry.from_wkt(wkt2)
     return g1.shape.equals(g2.shape)