def test_add_vector(self): # Test non existing raster null_path = os.path.join(NIAMOTO_HOME, "NULL.shp") self.assertRaises( FileNotFoundError, VectorManager.add_vector, "null_vector", null_path, ) VectorManager.add_vector("ncl_adm1", SHP_TEST) self.assertRaises( RecordAlreadyExistsError, VectorManager.add_vector, "ncl_adm1", SHP_TEST, ) df = VectorManager.get_vector_list() self.assertEqual(len(df), 1) self.assertEqual(df['name'].iloc[0], 'ncl_adm1') engine = Connector.get_engine() inspector = Inspector.from_engine(engine) self.assertIn( 'ncl_adm1', inspector.get_table_names(schema=settings.NIAMOTO_VECTOR_SCHEMA), )
def test_delete_vector(self): VectorManager.add_vector('ncl_adm1', SHP_TEST) VectorManager.delete_vector('ncl_adm1') df = VectorManager.get_vector_list() self.assertNotIn('ncl_adm1', list(df['name'])) engine = Connector.get_engine() inspector = Inspector.from_engine(engine) self.assertNotIn( 'ncl_adm1', inspector.get_table_names(schema=settings.NIAMOTO_VECTOR_SCHEMA), ) self.assertRaises(NoRecordFoundError, VectorManager.delete_vector, 'ncl_adm1')
def __init__(self, vector_name, label_col='label'): self.vector_name = vector_name pk_cols = VectorManager.get_vector_primary_key_columns(vector_name) self.geom_col = VectorManager.get_geometry_column(vector_name) self.pk_cols = [i[0] for i in pk_cols] tbl = VectorManager.get_vector_sqlalchemy_table(vector_name) columns = [c.copy() for c in tbl.columns if c.name not in self.pk_cols] super(VectorDimension, self).__init__( vector_name, columns, publisher=VectorDataPublisher(), label_col=label_col, )
def delete_vector(name): """ Delete an existing vector. :param name: The name of the vector. """ result = VectorManager.delete_vector(name) fix_db_sequences() return result
def add_vector(name, vector_file_path): """ Add a vector in database and register it the Niamoto vector registry. Uses ogr2ogr. All vectors are stored in the settings.NIAMOTO_RASTER_SCHEMA schema. :param name: The name of the vector. The created table will have this name. :param vector_file_path: The path to the vector file. """ return VectorManager.add_vector( name, vector_file_path, )
def get_elevation_vector_classes(request): """ Retrieve elevation vector classes. """ geojson = request.POST.get('geojson') df = VectorManager.get_vector_geo_dataframe( 'elevation_classes', geojson_filter=geojson, geojson_cut=True, ) return Response({ 'geojson': df.to_json() })
def update_vector(name, vector_file_path=None, new_name=None, properties=None): """ Update an existing vector in database and update it the Niamoto vector registry. Uses ogr2ogr. All vectors are stored in the settings.NIAMOTO_RASTER_SCHEMA schema. :param name: The name of the vector. :param vector_file_path: The path to the vector file. If None, the vector data won't be updated. :param new_name: The new name of the vector (not changed if None). :param properties: A dict of arbitrary properties. """ return VectorManager.update_vector(name, vector_file_path, new_name=new_name, properties=properties)
def get_vector_list(): """ :return: A pandas DataFrame containing all the vector available in the database. """ return VectorManager.get_vector_list()
def test_update_vector(self): # Add raster VectorManager.add_vector('ncl_adm1', SHP_TEST) # Update raster VectorManager.update_vector('ncl_adm1', SHP_TEST, new_name='ncl') VectorManager.update_vector('ncl', SHP_TEST) self.assertRaises( NoRecordFoundError, VectorManager.update_vector, 'ncl_adm1', SHP_TEST, ) null_path = os.path.join(NIAMOTO_HOME, "NULL.shp") self.assertRaises( FileNotFoundError, VectorManager.add_vector, "ncl_bis", null_path, ) VectorManager.add_vector('ncl_adm', SHP_TEST) self.assertRaises(RecordAlreadyExistsError, VectorManager.update_vector, 'ncl', SHP_TEST, new_name='ncl_adm') df = VectorManager.get_vector_list() self.assertIn('ncl', list(df['name'])) engine = Connector.get_engine() inspector = Inspector.from_engine(engine) self.assertIn( 'ncl', inspector.get_table_names(schema=settings.NIAMOTO_VECTOR_SCHEMA), ) self.assertNotIn( 'ncl_adm1', inspector.get_table_names(schema=settings.NIAMOTO_VECTOR_SCHEMA), ) VectorManager.update_vector('ncl', )
def test_get_vector_list(self): df1 = VectorManager.get_vector_list() self.assertEqual(len(df1), 0)
def test_get_vector_sqlalchemy_table(self): VectorManager.add_vector('ncl_adm1', SHP_TEST) table = VectorManager.get_vector_sqlalchemy_table('ncl_adm1') self.assertIsInstance(table, Table) self.assertEqual(table.name, 'ncl_adm1')
def test_get_vector_geo_dataframe(self): VectorManager.add_vector('ncl_adm1', SHP_TEST) df = VectorManager.get_vector_geo_dataframe('ncl_adm1') self.assertIsInstance(df, gpd.GeoDataFrame)
def test_get_vector_primary_key_columns(self): VectorManager.add_vector('ncl_adm1', SHP_TEST) pk_cols = VectorManager.get_vector_primary_key_columns('ncl_adm1') self.assertEqual(pk_cols, [('ogc_fid', 'integer')])
def test_get_geometry_column(self): VectorManager.add_vector('ncl_adm1', SHP_TEST) geom_col = VectorManager.get_geometry_column('ncl_adm1') self.assertEqual(geom_col, ('wkb_geometry', 'MULTIPOLYGON', 4326))
def get_values(self): """ :return: A dataframe containing the values stored in database for the dimension. """ return VectorManager.get_vector_geo_dataframe(self.name)
def _process(self, vector_name, *args, **kwargs): """ :param vector_name: The vector name in Niamoto vector database. :return: A GeoDataFrame corresponding to the vector to publish. """ return VectorManager.get_vector_geo_dataframe(vector_name)