def test_get_keys(self, path=None): if path is not None: env.DIR_GEOMCABINET = path sc = GeomCabinet() ret = list(sc.keys()) target_keys = ['state_boundaries'] self.assertEqual(len(set(target_keys).intersection(set(ret))), len(target_keys))
def test_misc_sql_subset(self): sc = GeomCabinet() path = sc.get_shp_path('state_boundaries') ds = ogr.Open(path) ret = ds.ExecuteSQL("select * from state_boundaries where state_name = 'New Jersey'") ret.ResetReading() self.assertEqual(len(ret), 1)
def test_iter_geoms_no_load_geoms(self): sc = GeomCabinet() it = sc.iter_geoms('state_boundaries', load_geoms=False) geoms = list(it) self.assertEqual(len(geoms), 51) self.assertEqual(geoms[12]['properties']['STATE_NAME'], 'New Hampshire') for geom in geoms: self.assertNotIn('geom', geom)
def test_iter_geoms_slice(self): """Test iteration providing a slice.""" g = GeomCabinet() slices = [3, slice(4, 7), [2, 6, 9, 40]] lengths = [1, 3, 4] for idx, slc in enumerate(slices): records = list(g.iter_geoms('state_boundaries', slc=slc)) self.assertEqual(len(records), lengths[idx])
def test_using_shp_path(self): # pass a path to a shapefile as opposed to a key path = GeomCabinet().get_shp_path('state_boundaries') ocgis.env.set_geomcabinet_path(None) # make sure there is path associated with the GeomCabinet with self.assertRaises(ValueError): list(GeomCabinet().keys()) g = Geom(path) self.assertEqual(g._shp_key, path) self.assertEqual(len(list(g.value)), 51)
def test_init(self): bp = '/a/bad/location' with self.assertRaises(ValueError): cabinet = GeomCabinet(bp) list(cabinet.iter_geoms('state_boundaries')) try: ocgis.env.set_geomcabinet_path(None) with self.assertRaises(ValueError): list(GeomCabinet().iter_geoms('state_boundaries')) finally: ocgis.env.reset()
def test_number_in_shapefile_name(self): """Test number in shapefile name.""" sc = GeomCabinet() path = sc.get_shp_path('state_boundaries') out_path = os.path.join(self.current_dir_output, '51_states.shp') with fiona.open(path) as source: with fiona.open(out_path, mode='w', driver='ESRI Shapefile', schema=source.meta['schema'], crs=source.meta['crs']) as sink: for record in source: sink.write(record) ret = list(GeomCabinetIterator(select_uid=[23], path=out_path)) self.assertEqual(len(ret), 1)
def test_get_features_object_file_geodatabase(self): ds = mock.create_autospec(ogr.DataSource, spec_set=True) m_Driver = mock.Mock() m_Driver.GetName = mock.Mock(return_value='OpenFileGDB') ds.GetDriver = mock.Mock(return_value=m_Driver) ds.GetLayerByName = mock.Mock() with self.assertRaises(ValueError): _ = GeomCabinet._get_features_object_(ds) desired = {'feature_class': 'foo'} _ = GeomCabinet._get_features_object_(ds, driver_kwargs=desired) ds.GetLayerByName.assert_called_once_with(desired['feature_class'])
def test_iter_geoms(self): sc = GeomCabinet() it = sc.iter_geoms('state_boundaries') geoms = list(it) self.assertEqual(len(geoms), 51) self.assertEqual(geoms[12]['properties']['STATE_NAME'], 'New Hampshire') for geom in geoms: self.assertIn(type(geom['geom']), (Polygon, MultiPolygon)) # Test with a shapefile not having a unique identifier. env.DEFAULT_GEOM_UID = 'ggidd' new = self.get_shapefile_path_with_no_ugid() sc = GeomCabinet() target = list(sc.iter_geoms(path=new)) self.assertEqual(len(target), 11) self.assertEqual(target[0]['properties'][env.DEFAULT_GEOM_UID], 0) self.assertEqual(target[3]['properties'][env.DEFAULT_GEOM_UID], 3) target = list(sc.iter_geoms(path=new, uid='ID')) self.assertNotIn(env.DEFAULT_GEOM_UID, target[9]['properties']) self.assertEqual(int, type(target[7]['properties']['ID'])) target = list(sc.iter_geoms(path=new, uid='ID', as_field=True)) ref = target[4] self.assertIsInstance(ref, Field) # Test with a different geometry unique identifier. path = self.get_shapefile_path_with_no_ugid() geom_select_uid = [12, 15] geom_uid = 'ID' sc = GeomCabinet() records = list(sc.iter_geoms(path=path, uid=geom_uid, select_uid=geom_select_uid)) self.assertEqual(len(records), 2) self.assertEqual([r['properties']['ID'] for r in records], geom_select_uid)
def test_process_name(self): copy_path = os.path.join(self.current_dir_output, 'test_shp_process') sc = GeomCabinet() test_path = os.path.split(sc.get_shp_path('wc_4326'))[0] shutil.copytree(test_path, copy_path) shp_path = os.path.join(copy_path, 'wc_4326.shp') out_folder = tempfile.mkdtemp(dir=self.current_dir_output) sp = ShpProcess(shp_path, out_folder) sp.process(key='world_countries', ugid=None, name='new_id') path = os.path.join(out_folder, 'world_countries.shp') with fiona.open(path, 'r') as sci: uids = [record['properties']['new_id'] for record in sci] self.assertEqual(uids, list(range(1, 212)))
def _run_(s, func): try: ds = ogr.Open(path) obj = GeomCabinet._get_features_object_(ds, select_sql_where=s) func(obj) finally: ds.Destroy()
def test_iteration_by_path(self): # test that a shapefile may be retrieved by passing a full path to the file path = GeomCabinet().get_shp_path('state_boundaries') ocgis.env.DIR_GEOMCABINET = None sci = GeomCabinetIterator(path=path) self.assertEqual(len(list(sci)), 51) for geom in sci: self.assertIn(type(geom['geom']), (Polygon, MultiPolygon))
def test_shp_process(self): copy_path = os.path.join(self.current_dir_output, 'test_shp_process') sc = GeomCabinet() test_path = os.path.split(sc.get_shp_path('wc_4326'))[0] shutil.copytree(test_path, copy_path) shp_path = os.path.join(copy_path, 'wc_4326.shp') out_folder = tempfile.mkdtemp(dir=self.current_dir_output) sp = ShpProcess(shp_path, out_folder) sp.process(key='world_countries', ugid=None) sc = GeomCabinet(path=out_folder) select_ugid = [33, 126, 199] geoms = list(sc.iter_geoms('world_countries', select_uid=select_ugid)) self.assertEqual(len(geoms), 3) names = [item['properties']['NAME'] for item in geoms] self.assertEqual(set(names), set(['Canada', 'Mexico', 'United States']))
def get_shapefile_path_no_default_unique_identifier(self): path_sink = self.get_temporary_file_path('no_uid.shp') path_source = GeomCabinet().get_shp_path('state_boundaries') with fiona.open(path_source) as source: sink_meta = source.meta sink_meta['schema']['properties'].pop('UGID') with fiona.open(path_sink, mode='w', **sink_meta) as sink: for record in source: record['properties'].pop('UGID') sink.write(record) return path_sink
def test_validate(self): # snippets should be allowed for field objects field = self.test_data.get_rd('cancm4_tas').get() ops = OcgOperations(dataset=field, snippet=True) self.assertTrue(ops.snippet) # test driver validation is called appropriately path = GeomCabinet().get_shp_path('state_boundaries') rd = RequestDataset(path) with self.assertRaises(DefinitionValidationError): OcgOperations(dataset=rd, output_format='csv')
def test_get_features_object(self): # Test with a shapefile not having the default unique geometry identifier path = self.get_shapefile_path_with_no_ugid() keywords = dict(uid=[None, 'ID'], select_uid=[None, [8, 11, 13]], select_sql_where=[None, "STATE_NAME = 'Hawaii'"]) for k in self.iter_product_keywords(keywords): # print(k) ds = ogr.Open(path) try: try: obj = GeomCabinet._get_features_object_( ds, uid=k.uid, select_uid=k.select_uid, select_sql_where=k.select_sql_where) except RuntimeError: self.assertIsNone(k.uid) self.assertIsNotNone(k.select_uid) continue if k.select_sql_where is not None: length = 1 elif k.select_uid is not None: length = 3 else: length = 11 self.assertEqual(len(obj), length) self.assertIsInstance(obj, Layer) finally: ds.Destroy() # Test on a shapefile having the default unique geometry identifier path = GeomCabinet().get_shp_path('state_boundaries') ds = ogr.Open(path) try: obj = GeomCabinet._get_features_object_(ds, select_uid=[8, 11, 13]) self.assertEqual(len(obj), 3) finally: ds.Destroy()
def test_len(self): path = GeomCabinet().get_shp_path('state_boundaries') sci = GeomCabinetIterator(path=path) self.assertEqual(len(sci), 51) sci = GeomCabinetIterator(path=path, select_uid=[16, 19]) self.assertEqual(len(sci), 2) sci = GeomCabinetIterator(key='state_boundaries', select_sql_where="STATE_NAME = 'Vermont'") self.assertEqual(len(sci), 1) sci = GeomCabinetIterator(key='state_boundaries', slc=[4, 10, 20]) self.assertEqual(len(sci), 3)
def test_get_features_object(self): # Test with a shapefile not having the default unique geometry identifier path = self.get_shapefile_path_with_no_ugid() keywords = dict(uid=[None, 'ID'], select_uid=[None, [8, 11, 13]], select_sql_where=[None, "STATE_NAME = 'Hawaii'"]) for k in self.iter_product_keywords(keywords): # print(k) ds = ogr.Open(path) try: try: obj = GeomCabinet._get_features_object_(ds, uid=k.uid, select_uid=k.select_uid, select_sql_where=k.select_sql_where) except RuntimeError: self.assertIsNone(k.uid) self.assertIsNotNone(k.select_uid) continue if k.select_sql_where is not None: length = 1 elif k.select_uid is not None: length = 3 else: length = 11 self.assertEqual(len(obj), length) self.assertIsInstance(obj, Layer) finally: ds.Destroy() # Test on a shapefile having the default unique geometry identifier path = GeomCabinet().get_shp_path('state_boundaries') ds = ogr.Open(path) try: obj = GeomCabinet._get_features_object_(ds, select_uid=[8, 11, 13]) self.assertEqual(len(obj), 3) finally: ds.Destroy()
def test_get_features_object_select_sql_where(self): path = GeomCabinet().get_shp_path('state_boundaries') def _run_(s, func): try: ds = ogr.Open(path) obj = GeomCabinet._get_features_object_(ds, select_sql_where=s) func(obj) finally: ds.Destroy() s = "STATE_NAME in ('Wisconsin', 'Vermont')" def f(obj): self.assertEqual(len(obj), 2) self.assertAsSetEqual([ii.items()['STATE_NAME'] for ii in obj], ("Wisconsin", "Vermont")) _run_(s, f) s = "STATE_NAME in ('Wisconsin', 'Vermont') and STATE_ABBR in ('NV', 'OH')" def f(obj): self.assertEqual(len(obj), 0) _run_(s, f) s = "STATE_NAME in ('Wisconsin', 'Vermont') or STATE_ABBR in ('NV', 'OH')" def f(obj): self.assertEqual(len(obj), 4) _run_(s, f) s = "STATE_NAMEE in ('Wisconsin', 'Vermont')" with self.assertRaises(RuntimeError): _run_(s, lambda x: None) s = 'UGID > 40' def f(obj): self.assertEqual(len(obj), 11) for ii in obj: item = ii.items()['UGID'] self.assertTrue(item > 40) _run_(s, f)
def test_iter_geoms_select_sql_where(self): sc = GeomCabinet() sql = "STATE_NAME = 'New Hampshire'" self.assertEqual(len(list(sc.iter_geoms('state_boundaries', select_sql_where=sql))), 1)
def test_iter_geoms_select_ugid(self): sc = GeomCabinet() it = sc.iter_geoms('state_boundaries', select_uid=[13]) geoms = list(it) self.assertEqual(len(geoms), 1) self.assertEqual(geoms[0]['properties']['STATE_NAME'], 'New Hampshire')
def test_iter_geoms_select_sql_where(self): sc = GeomCabinet() sql = "STATE_NAME = 'New Hampshire'" self.assertEqual( len(list(sc.iter_geoms('state_boundaries', select_sql_where=sql))), 1)
def test_iter_geoms(self): sc = GeomCabinet() it = sc.iter_geoms('state_boundaries') geoms = list(it) self.assertEqual(len(geoms), 51) self.assertEqual(geoms[12]['properties']['STATE_NAME'], 'New Hampshire') for geom in geoms: self.assertIn(type(geom['geom']), (Polygon, MultiPolygon)) # Test with a shapefile not having a unique identifier. env.DEFAULT_GEOM_UID = 'ggidd' new = self.get_shapefile_path_with_no_ugid() sc = GeomCabinet() target = list(sc.iter_geoms(path=new)) self.assertEqual(len(target), 11) self.assertEqual(target[0]['properties'][env.DEFAULT_GEOM_UID], 0) self.assertEqual(target[3]['properties'][env.DEFAULT_GEOM_UID], 3) target = list(sc.iter_geoms(path=new, uid='ID')) self.assertNotIn(env.DEFAULT_GEOM_UID, target[9]['properties']) self.assertEqual(int, type(target[7]['properties']['ID'])) target = list(sc.iter_geoms(path=new, uid='ID', as_field=True)) ref = target[4] self.assertIsInstance(ref, Field) # Test with a different geometry unique identifier. path = self.get_shapefile_path_with_no_ugid() geom_select_uid = [12, 15] geom_uid = 'ID' sc = GeomCabinet() records = list( sc.iter_geoms(path=path, uid=geom_uid, select_uid=geom_select_uid)) self.assertEqual(len(records), 2) self.assertEqual([r['properties']['ID'] for r in records], geom_select_uid)