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')
Example #3
0
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 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_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))