def test_fid(fid_source, fid_field, id_expect, ngw_resource_group, ngw_txn): src = Path(__file__).parent / 'data' / 'type.geojson' dataset = ogr.Open(str(src)) assert dataset is not None, gdal.GetLastErrorMsg() layer = dataset.GetLayer(0) assert layer is not None, gdal.GetLastErrorMsg() res = VectorLayer(parent_id=ngw_resource_group, display_name='test_fid', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex) res.persist() res.setup_from_ogr(layer, fid_params=dict(fid_source=fid_source, fid_field=fid_field)) res.load_from_ogr(layer) DBSession.flush() query = res.feature_query() query.filter_by(id=id_expect) assert query().total_count == 1
def test_id_field(data, ngw_resource_group): res = VectorLayer(parent_id=ngw_resource_group, display_name=f'test-{data}', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex).persist() src = os.path.join(DATA_PATH, f'{data}.geojson') ds = ogr.Open(src) layer = ds.GetLayer(0) with pytest.raises(ValidationError): res.setup_from_ogr(layer) fid_params = dict(fid_source=FID_SOURCE.FIELD, fid_field=['id']) with pytest.raises(ValidationError): res.setup_from_ogr(layer, fid_params=fid_params) res.setup_from_ogr(layer, fix_errors=ERROR_FIX.SAFE, fid_params=fid_params) res.load_from_ogr(layer) DBSession.flush() query = res.feature_query() feature = query().one() assert feature.id == 1 assert list(feature.fields.keys()) == ['id_1']
def test_from_fields(ngw_resource_group, ngw_txn): res = VectorLayer( parent_id=ngw_resource_group, display_name='from_fields', owner_user=User.by_keyname('administrator'), geometry_type='POINT', srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ) res.setup_from_fields([ dict(keyname='integer', datatype=FIELD_TYPE.INTEGER), dict(keyname='bigint', datatype=FIELD_TYPE.BIGINT), dict(keyname='real', datatype=FIELD_TYPE.REAL), dict(keyname='string', datatype=FIELD_TYPE.STRING, label_field=True), dict(keyname='date', datatype=FIELD_TYPE.DATE), dict(keyname='time', datatype=FIELD_TYPE.TIME), dict(keyname='datetime', datatype=FIELD_TYPE.DATETIME), ]) res.persist() assert res.feature_label_field.keyname == 'string' DBSession.flush()
def vector_layer_id(ngw_resource_group): with transaction.manager: obj = VectorLayer( parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { 'type': 'FeatureCollection', 'features': get_features_for_orderby_test() } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) obj.setup_from_ogr(layer) obj.load_from_ogr(layer) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=obj.id).one())
def type_layer(ngw_resource_group): with transaction.manager: vl_type = VectorLayer( parent_id=ngw_resource_group, display_name='type', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() import nextgisweb.vector_layer.test path = Path( nextgisweb.vector_layer.test.__file__).parent / 'data/type.geojson' ogrds = ogr.Open(str(path)) ogrlayer = ogrds.GetLayer(0) vl_type.setup_from_ogr(ogrlayer) vl_type.load_from_ogr(ogrlayer) DBSession.flush() DBSession.expunge(vl_type) yield vl_type.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=vl_type.id).one())
def test_from_ogr(data, ngw_resource_group, ngw_txn): src = os.path.join(DATA_PATH, data) dsource = ogr.Open('/vsizip/' + src) layer = dsource.GetLayer(0) res = VectorLayer( parent_id=ngw_resource_group, display_name='from_ogr', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ) res.persist() res.setup_from_ogr(layer, lambda x: x) res.load_from_ogr(layer, lambda x: x) DBSession.flush() features = list(res.feature_query()()) assert len(features) == 1 feature = features[0] assert feature.id == 1 fields = feature.fields assert fields['int'] == -1 # TODO: Date, time and datetime tests fails on shapefile # assert fields['date'] == date(2001, 1, 1) # assert fields['time'] == time(23, 59, 59) # assert fields['datetime'] == datetime(2001, 1, 1, 23, 59, 0) assert fields['string'] == "Foo bar" assert fields[ 'unicode'] == 'Значимость этих проблем настолько очевидна, что реализация намеченных плановых заданий требуют определения и уточнения.' # NOQA: E501
def vector_layer_id(ngw_resource_group): with transaction.manager: obj = VectorLayer( parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { "type": "FeatureCollection", "name": "polygon_extent", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::3857"}}, "features": [ { "type": "Feature", "properties": {"name": "west"}, "geometry": { "type": "Polygon", "coordinates": [[ [5542180, 8799167], [6191082, 7551279], [4668659, 7126998], [5542180, 8799167], ]], }, }, { "type": "Feature", "properties": {"name": "east"}, "geometry": { "type": "Polygon", "coordinates": [[ [15100999, 10396463], [16498633, 10546209], [16673337, 9223449], [15175872, 8948913], [15100999, 10396463], ]], }, }, ], } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) obj.setup_from_ogr(layer) obj.load_from_ogr(layer) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=obj.id).one())
def vlayer_id(ngw_resource_group): with transaction.manager: res_vl = VectorLayer( parent_id=ngw_resource_group, display_name='test_cyrillic', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { 'type': 'FeatureCollection', 'crs': { 'type': 'name', 'properties': { 'name': 'urn:ogc:def:crs:EPSG::3857' } }, 'features': [{ 'type': 'Feature', 'properties': { 'field1': 1, 'поле2': 'значение1', '!field3': '!@#$%^&*()значение1' }, 'geometry': { 'type': 'Point', 'coordinates': [0, 0] } }, { 'type': 'Feature', 'properties': { 'field1': 2, 'поле2': 'значение2', '!field3': '!@#$%^&*()значение2' }, 'geometry': { 'type': 'Point', 'coordinates': [10, 10] } }] } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) res_vl.setup_from_ogr(layer) res_vl.load_from_ogr(layer) DBSession.flush() DBSession.expunge(res_vl) yield res_vl.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=res_vl.id).one())
def test_create(filename, options, checks, ngw_resource_group, ngw_txn): obj = VectorLayer(parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex)).persist() src = str(path / filename) ds = ogr.Open(src) layer = ds.GetLayer(0) geom_cast_params = dict(geometry_type=options.get('geometry_type'), is_multi=options.get('is_multi'), has_z=options.get('has_z')) def setup_and_load(): setup_kwargs = dict() load_kwargs = dict() if 'skip_other_geometry_types' in options: setup_kwargs['skip_other_geometry_types'] = options[ 'skip_other_geometry_types'] load_kwargs['skip_other_geometry_types'] = options[ 'skip_other_geometry_types'] if 'fix_errors' in options: load_kwargs['fix_errors'] = options['fix_errors'] if 'skip_errors' in options: load_kwargs['skip_errors'] = options['skip_errors'] obj.setup_from_ogr(layer, geom_cast_params=geom_cast_params, **setup_kwargs) obj.load_from_ogr(layer, **load_kwargs) if 'exception' in checks: with pytest.raises(checks['exception']): setup_and_load() DBSession.expunge(obj) else: setup_and_load() DBSession.flush() if 'geometry_type' in checks: exp_geometry_type = checks['geometry_type'] assert obj.geometry_type == exp_geometry_type, \ "Expected geometry type was {} but actually got {}".format( exp_geometry_type, obj.geometry_type) if 'feature_count' in checks: exp_feature_count = checks['feature_count'] query = obj.feature_query() feature_count = query().total_count assert feature_count == exp_feature_count, \ "Expected feature count was {} but got {}".format( exp_feature_count, feature_count)
def vector_layer(display_name, parent_id): res = VectorLayer( parent_id=parent_id, display_name=display_name, owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), geometry_type='POINT', tbl_uuid=six.text_type(uuid4().hex), ).persist() res.setup_from_fields([]) return res
def vector_layer_id(ngw_resource_group): with transaction.manager: obj = VectorLayer( parent_id=ngw_resource_group, display_name='vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() geojson = { 'type': 'FeatureCollection', 'crs': { 'type': 'name', 'properties': { 'name': 'urn:ogc:def:crs:EPSG::3857' } }, 'features': [{ 'type': 'Feature', 'properties': { 'price': -1, 'name': 'feature1' }, 'geometry': { 'type': 'Point', 'coordinates': [0, 0] } }, { 'type': 'Feature', 'properties': { 'price': -2, 'name': 'feature2' }, 'geometry': { 'type': 'Point', 'coordinates': [0, 0] } }] } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) obj.setup_from_ogr(layer) obj.load_from_ogr(layer) DBSession.flush() DBSession.expunge(obj) yield obj.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=obj.id).one())
def service_id(ngw_resource_group): with transaction.manager: res_vl = VectorLayer( parent_id=ngw_resource_group, display_name='test_vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() geojson = { 'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'urn:ogc:def:crs:EPSG::3857'}}, 'features': [{ 'type': 'Feature', 'properties': {'name': 'feature1'}, 'geometry': {'type': 'Point', 'coordinates': [0, 0]} }, { 'type': 'Feature', 'properties': {'price': -1}, 'geometry': {'type': 'Point', 'coordinates': [10, 10]} }] } dsource = ogr.Open(json.dumps(geojson)) layer = dsource.GetLayer(0) res_vl.setup_from_ogr(layer, lambda x: x) res_vl.load_from_ogr(layer, lambda x: x) DBSession.flush() res_wfs = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() res_wfs.layers.append(WFSLayer( resource=res_vl, keyname='test', display_name='test', maxfeatures=1000, )) DBSession.flush() DBSession.expunge(res_vl) DBSession.expunge(res_wfs) yield res_wfs.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=res_vl.id).one()) DBSession.delete(WFSService.filter_by(id=res_wfs.id).one())
def frtc(ngw_resource_group, ngw_txn): vector_layer = VectorLayer(parent_id=ngw_resource_group, display_name='from_fields', owner_user=User.by_keyname('administrator'), geometry_type='POINT', srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex)).persist() vector_layer.setup_from_fields([]) result = ResourceTileCache(resource=vector_layer, ).persist() DBSession.flush() result.initialize() return result
def test_type_geojson(ngw_resource_group, ngw_txn): src = Path(__file__).parent / 'data' / 'type.geojson' dataset = ogr.Open(str(src)) assert dataset is not None, gdal.GetLastErrorMsg() layer = dataset.GetLayer(0) assert layer is not None, gdal.GetLastErrorMsg() res = VectorLayer(parent_id=ngw_resource_group, display_name='from_ogr', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex)) res.persist() res.setup_from_ogr(layer, lambda x: x) res.load_from_ogr(layer, lambda x: x) layer.ResetReading() DBSession.flush() def field_as(f, n, t): fidx = f.GetFieldIndex(n) if f.IsFieldNull(fidx): return None attr = getattr(f, 'GetFieldAs' + t) result = attr(fidx) if t in ('Date', 'Time', 'DateTime'): result = [int(v) for v in result] if t == 'String' and six.PY2: result = result.decode('utf-8') return result for feat, ref in zip(res.feature_query()(), layer): fields = feat.fields assert fields['null'] == field_as(ref, 'null', None) assert fields['int'] == field_as(ref, 'int', 'Integer') assert fields['real'] == field_as(ref, 'real', 'Double') assert fields['date'] == date(*field_as(ref, 'date', 'DateTime')[0:3]) assert fields['time'] == time(*field_as(ref, 'time', 'DateTime')[3:6]) assert fields['datetime'] == datetime( *field_as(ref, 'datetime', 'DateTime')[0:6]) assert fields['string'] == field_as(ref, 'string', 'String') assert fields['unicode'] == field_as(ref, 'unicode', 'String')
def create_feature_layer(ogrlayer, parent_id, **kwargs): with transaction.manager: layer = VectorLayer( parent_id=parent_id, display_name='Feature layer (vector)', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex, ).persist() layer.setup_from_ogr(ogrlayer) layer.load_from_ogr(ogrlayer) DBSession.flush() yield layer
def wfs_service_path(ngw_resource_group, ngw_httptest_app): with transaction.manager: vl_type = VectorLayer( parent_id=ngw_resource_group, display_name='type', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() dsource = type_geojson_dataset('type.geojson') layer = dsource.GetLayer(0) vl_type.setup_from_ogr(layer, lambda x: x) vl_type.load_from_ogr(layer, lambda x: x) DBSession.flush() # NOTE: GDAL doesn't support time fields in GML / WFS. It completely breaks # XSD schema parsing. Delete the time field to pass tests. DBSession.delete(vl_type.field_by_keyname('time')) wfs_service = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() wfs_service.layers.append( WFS_Service_Layer(resource=vl_type, keyname='type', display_name='type', maxfeatures=1000)) DBSession.flush() DBSession.expunge(vl_type) DBSession.expunge(wfs_service) path = '{}/api/resource/{}/wfs'.format(ngw_httptest_app.base_url, wfs_service.id) yield path with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=vl_type.id).one()) DBSession.delete(WFSService.filter_by(id=wfs_service.id).one())
def test_from_ogr(txn, data): src = os.path.join(DATA_PATH, data) dsource = ogr.Open(src) layer = dsource.GetLayer(0) res = VectorLayer( parent_id=0, display_name='from_ogr', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=unicode(uuid4().hex), ) res.persist() res.setup_from_ogr(layer, lambda x: x) res.load_from_ogr(layer, lambda x: x) DBSession.flush()
def service(ngw_resource_group): with transaction.manager: res_vl = VectorLayer( parent_id=ngw_resource_group, display_name='test_vector_layer', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() dsource = type_geojson_dataset() layer = dsource.GetLayer(0) res_vl.setup_from_ogr(layer, lambda x: x) res_vl.load_from_ogr(layer, lambda x: x) DBSession.flush() res_wfs = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() res_wfs.layers.append( WFSLayer( resource=res_vl, keyname='test', display_name='test', maxfeatures=1000, )) DBSession.flush() DBSession.expunge(res_vl) DBSession.expunge(res_wfs) yield res_wfs.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=res_vl.id).one()) DBSession.delete(WFSService.filter_by(id=res_wfs.id).one())
def resource(ngw_txn, ngw_resource_group): src = os.path.join(DATA_PATH, 'geojson-point.zip/layer.geojson') dsource = ogr.Open('/vsizip/' + src) layer = dsource.GetLayer(0) resource = VectorLayer( parent_id=ngw_resource_group, display_name='from_ogr', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ) resource.persist() resource.setup_from_ogr(layer, lambda x: x) resource.load_from_ogr(layer, lambda x: x) DBSession.flush() return resource
def test_error_limit(ngw_resource_group): res = VectorLayer(parent_id=ngw_resource_group, display_name='error-limit', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex) res.persist() ds = ogr.GetDriverByName('Memory').CreateDataSource('') srs = osr.SpatialReference() srs.ImportFromEPSG(4326) layer = ds.CreateLayer('layer_with_errors', srs=srs, geom_type=ogr.wkbPoint) defn = layer.GetLayerDefn() some = 3 for i in range(error_limit + some): feature = ogr.Feature(defn) if i < error_limit: feature.SetGeometry(None) else: feature.SetGeometry(ogr.CreateGeometryFromWkt('POINT (0 0)')) layer.CreateFeature(feature) res.setup_from_ogr(layer) opts = dict(fix_errors=ERROR_FIX.NONE, skip_other_geometry_types=False) with pytest.raises(ValidationError) as excinfo: res.load_from_ogr(layer, **opts, skip_errors=False) assert excinfo.value.detail is not None res.load_from_ogr(layer, **opts, skip_errors=True) DBSession.flush() assert res.feature_query()().total_count == some
def test_geom_field(ngw_resource_group): res = VectorLayer(parent_id=ngw_resource_group, display_name='test-geom-fld', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=uuid4().hex).persist() src = os.path.join(DATA_PATH, 'geom-fld.geojson') ds = ogr.Open(src) layer = ds.GetLayer(0) with pytest.raises(ValidationError): res.setup_from_ogr(layer) res.setup_from_ogr(layer, fix_errors=ERROR_FIX.SAFE) res.load_from_ogr(layer) DBSession.flush() query = res.feature_query() feature = query().one() assert feature.id == 1 assert list(feature.fields.keys()) == ['geom_1']
def test_from_fields(txn): res = VectorLayer( parent_id=0, display_name='from_fields', owner_user=User.by_keyname('administrator'), geometry_type='POINT', srs=SRS.filter_by(id=3857).one(), tbl_uuid=unicode(uuid4().hex), ) res.setup_from_fields([ dict(keyname='integer', datatype=FIELD_TYPE.INTEGER), dict(keyname='bigint', datatype=FIELD_TYPE.BIGINT), dict(keyname='real', datatype=FIELD_TYPE.REAL), dict(keyname='string', datatype=FIELD_TYPE.STRING), dict(keyname='date', datatype=FIELD_TYPE.DATE), dict(keyname='time', datatype=FIELD_TYPE.TIME), dict(keyname='datetime', datatype=FIELD_TYPE.DATETIME), ]) res.persist() DBSession.flush()
def frtc(ngw_resource_group): with transaction.manager: vector_layer = VectorLayer(parent_id=ngw_resource_group, display_name='from_fields', owner_user=User.by_keyname('administrator'), geometry_type='POINT', srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type( uuid4().hex)).persist() vector_layer.setup_from_fields([]) result = ResourceTileCache(resource=vector_layer, ).persist() result.async_writing = True DBSession.flush() result.initialize() yield result with transaction.manager: DBSession.delete( ResourceTileCache.filter_by(resource_id=result.resource_id).one()) DBSession.delete(VectorLayer.filter_by(id=vector_layer.id).one())
def service(ngw_resource_group): with transaction.manager: vl_type = VectorLayer( parent_id=ngw_resource_group, display_name='type', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() dsource = type_geojson_dataset('type.geojson') layer = dsource.GetLayer(0) vl_type.setup_from_ogr(layer, lambda x: x) vl_type.load_from_ogr(layer, lambda x: x) DBSession.flush() # NOTE: GDAL doesn't support time fields in GML / WFS. It completely breaks # XSD schema parsing. Delete the time field to pass tests. DBSession.delete(vl_type.field_by_keyname('time')) vl_pointz = VectorLayer( parent_id=ngw_resource_group, display_name='pointz', owner_user=User.by_keyname('administrator'), srs=SRS.filter_by(id=3857).one(), tbl_uuid=six.text_type(uuid4().hex), ).persist() dsource = type_geojson_dataset('pointz.geojson') layer = dsource.GetLayer(0) vl_pointz.setup_from_ogr(layer, lambda x: x) vl_pointz.load_from_ogr(layer, lambda x: x) DBSession.flush() res_wfs = WFSService( parent_id=ngw_resource_group, display_name='test_wfsserver_service', owner_user=User.by_keyname('administrator'), ).persist() res_wfs.layers.extend(( WFSLayer(resource=vl_type, keyname='type', display_name='type', maxfeatures=1000), WFSLayer(resource=vl_pointz, keyname='pointz', display_name='pointz', maxfeatures=1000), )) DBSession.flush() DBSession.expunge(vl_type) DBSession.expunge(vl_pointz) DBSession.expunge(res_wfs) yield res_wfs.id with transaction.manager: DBSession.delete(VectorLayer.filter_by(id=vl_type.id).one()) DBSession.delete(VectorLayer.filter_by(id=vl_pointz.id).one()) DBSession.delete(WFSService.filter_by(id=res_wfs.id).one())