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_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_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_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_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 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_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)