def add_shapefile_unique_identifier(in_path, out_path, name=None, template=None): """ >>> add_shapefile_unique_identifier('/path/to/foo.shp', '/path/to/new_foo.shp') '/path/to/new_foo.shp' :param str in_path: Full path to the input shapefile. :param str out_path: Full path to the output shapefile. :param str name: The name of the unique identifer. If ``None``, defaults to :attr:`ocgis.constants.OCGIS_UNIQUE_GEOMETRY_IDENTIFIER`. :param str template: The integer attribute to copy as the unique identifier. :returns: Path to the copied shapefile with the addition of a unique integer attribute called ``name``. :rtype: str """ from ocgis.util.shp_process import ShpProcess out_folder, key = os.path.split(out_path) sp = ShpProcess(in_path, out_folder) key = os.path.splitext(key)[0] sp.process(key=key, ugid=template, name=name) return out_path
def prepare_shapefile(filename): from ocgis.util.shp_process import ShpProcess LOG.info('preparing shapefile --> ' + filename) shp_path = os.path.join(DIR_OUTPUT_LINUX, filename) sp = ShpProcess(shp_path) key = os.path.splitext(filename)[0] sp.process(DIR_OUTPUT_LINUX, key) LOG.info('success.')
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_shp_process(self): copy_path = os.path.join(self._test_dir,'test_shp_process') shutil.copytree(self._test_path,copy_path) shp_path = os.path.join(copy_path,'wc_4326.shp') out_folder = tempfile.mkdtemp(dir=self._test_dir) 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_ugid=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_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 test_shp_process(self): test_path = '/home/local/WX/ben.koziol/Dropbox/nesii/project/ocg/bin/test_data/test_shp_process' copy_path = os.path.join(self._test_dir,'test_shp_process') shutil.copytree(test_path,copy_path) shp_path = os.path.join(copy_path,'wc_4326.shp') sp = ShpProcess(shp_path) out_folder = tempfile.mkdtemp(dir=self._test_dir) sp.process(out_folder,'world_countries',ugid=None) sc = ShpCabinet(path=out_folder) select_ugid = [33,126,199] geoms = list(sc.iter_geoms('world_countries',select_ugid=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_shp_process(self): test_path = '/home/local/WX/ben.koziol/Dropbox/nesii/project/ocg/bin/test_data/test_shp_process' copy_path = os.path.join(self._test_dir, 'test_shp_process') shutil.copytree(test_path, copy_path) shp_path = os.path.join(copy_path, 'wc.shp') sp = ShpProcess(shp_path) out_folder = tempfile.mkdtemp(dir=self._test_dir) sp.process(out_folder, 'world_countries', ugid=None) sc = ShpCabinet(path=out_folder) select_ugid = [33, 126, 199] geoms = sc.get_geoms('world_countries', select_ugid=select_ugid) self.assertEqual(len(geoms), 3) names = [item['NAME'] for item in geoms] self.assertEqual(set(names), set(['Canada', 'Mexico', 'United States']))
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 add_shapefile_unique_identifier(in_path, out_path, name=None, template=None): """ >>> add_shapefile_unique_identifier('/path/to/foo.shp', '/path/to/new_foo.shp') '/path/to/new_foo.shp' :param str in_path: Full path to the input shapefile. :param str out_path: Full path to the output shapefile. :param str name: The name of the unique identifer. If ``None``, defaults to :attr:`ocgis.constants.OCGIS_UNIQUE_GEOMETRY_IDENTIFIER`. :param str template: The integer attribute to copy as the unique identifier. :returns: Path to the copied shapefile with the addition of a unique integer attribute called ``name``. :rtype: str """ out_folder, key = os.path.split(out_path) sp = ShpProcess(in_path, out_folder) key = os.path.splitext(key)[0] sp.process(key=key, ugid=template, name=name) return out_path
from ocgis.util.shp_process import ShpProcess shape_folders = '/home/local/WX/ben.koziol/Dropbox/nesii/project/ocg/bin/shp' logging.basicConfig(level=logging.INFO) log_fiona = logging.getLogger('Fiona') log_fiona.setLevel(logging.ERROR) output_folder = mkdtemp(prefix='shp') logging.info('output folder = ' + output_folder) for dirpath, dirnames, filenames in os.walk(shape_folders): for filename in filenames: if filename.endswith('.shp'): logging.info(filename) shp_path = os.path.join(dirpath, filename) cfg_path = shp_path.replace('.shp', '.cfg') key = filename.split('.')[0] config = SafeConfigParser() config.read(cfg_path) ugid = config.get('mapping', 'ugid') if ugid == 'none': ugid = None sp = ShpProcess(shp_path) try: sp.process(output_folder, key, ugid=ugid) except KeyError: sp.process(output_folder, key, ugid=ugid.upper()) new_cfg_path = os.path.join(output_folder, key, os.path.split(cfg_path)[1]) shutil.copy2(cfg_path, new_cfg_path) logging.info('success')
shape_folders = '/home/local/WX/ben.koziol/Dropbox/nesii/project/ocg/bin/shp' logging.basicConfig(level=logging.INFO) log_fiona = logging.getLogger('Fiona') log_fiona.setLevel(logging.ERROR) output_folder = mkdtemp(prefix='shp') logging.info('output folder = ' + output_folder) for dirpath, dirnames, filenames in os.walk(shape_folders): for filename in filenames: if filename.endswith('.shp'): logging.info(filename) shp_path = os.path.join(dirpath, filename) cfg_path = shp_path.replace('.shp', '.cfg') key = filename.split('.')[0] config = SafeConfigParser() config.read(cfg_path) ugid = config.get('mapping', 'ugid') if ugid == 'none': ugid = None sp = ShpProcess(shp_path) try: sp.process(output_folder, key, ugid=ugid) except KeyError: sp.process(output_folder, key, ugid=ugid.upper()) new_cfg_path = os.path.join(output_folder, key, os.path.split(cfg_path)[1]) shutil.copy2(cfg_path, new_cfg_path) logging.info('success')