def test_attribute_flags(self): attr_flags = ['none','all'] for attr_flag in attr_flags: ## make temporary directory path = get_temp_path(only_dir=True,nest=True) try: ## the shapefile key to use key = 'state_boundaries' ## get path to shapefile and copy directory to temporary location sc = ShpCabinet() origin = os.path.split(sc.get_shp_path(key))[0] shutil.copytree(origin,os.path.join(path,key)) ## remove original config file os.remove(os.path.join(path,key,key+'.cfg')) ## write config file config = ConfigParser.ConfigParser() config.add_section('mapping') config.set('mapping','ugid','none') config.set('mapping','attributes',attr_flag) with open(os.path.join(path,key,key+'.cfg'),'w') as f: config.write(f) ## load data sc = ShpCabinet(path) geoms = sc.get_geoms(key) for geom in geoms: if attr_flag == 'none': self.assertEqual(set(['ugid','geom']),set(geom.keys())) else: self.assertEqual(set(['ugid', 'state_name', 'state_fips', 'geom', 'state_abbr', 'id']), set(geom.keys())) finally: shutil.rmtree(path)
def test_sql_subset(self): sc = ShpCabinet() 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_sql_subset(self): sc = ShpCabinet() 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_shapefiles_not_in_folders(self): for dirpath,dirnames,filenames in os.walk(ocgis.env.DIR_SHPCABINET): for filename in filenames: if filename.startswith('state_boundaries') or filename.startswith('world_countries'): dst = os.path.join(self._test_dir,filename) src = os.path.join(dirpath,filename) shutil.copy2(src,dst) self.test_get_keys(dir_shpcabinet=self._test_dir) sc = ShpCabinet(path=self._test_dir) path = sc.get_shp_path('world_countries') self.assertEqual(path,os.path.join(self._test_dir,'world_countries.shp'))
def test_number_in_shapefile_name(self): """Test number in shapefile name.""" sc = ShpCabinet() 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(ShpCabinetIterator(select_uid=[23], path=out_path)) self.assertEqual(len(ret), 1)
def test_process_name(self): copy_path = os.path.join(self.current_dir_output, 'test_shp_process') sc = ShpCabinet() 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, range(1, 212))
def test_shapefiles_not_in_folders(self): for dirpath, dirnames, filenames in os.walk(ocgis.env.DIR_SHPCABINET): for filename in filenames: if filename.startswith( 'state_boundaries') or filename.startswith( 'world_countries'): dst = os.path.join(self._test_dir, filename) src = os.path.join(dirpath, filename) shutil.copy2(src, dst) self.test_get_keys(dir_shpcabinet=self._test_dir) sc = ShpCabinet(path=self._test_dir) path = sc.get_shp_path('world_countries') self.assertEqual(path, os.path.join(self._test_dir, 'world_countries.shp'))
def test_shp_process(self): copy_path = os.path.join(self.current_dir_output, 'test_shp_process') sc = ShpCabinet() 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 = ShpCabinet(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']))