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_system_with_callback(self): fp = get_temp_path(wd=self.current_dir_output) def callback(message, path=fp): with open(path, 'a') as sink: sink.write(message) sink.write('\n') class FooError(Exception): pass ocgis_lh.configure(callback=callback) ocgis_lh(msg='this is a test message') ocgis_lh() ocgis_lh(msg='this is a second test message') ocgis_lh(msg='this should not be there', level=logging.DEBUG) exc = FooError('foo message for value error') try: ocgis_lh(exc=exc) except FooError: pass with open(fp, 'r') as source: lines = source.readlines() self.assertEqual(lines, ['this is a test message\n', 'this is a second test message\n', 'FooError: foo message for value error\n'])
def get_shp(request, key=None): query = helpers.parse_qs(request.META['QUERY_STRING']) select_ugid = SelectUgid() select_ugid.parse_query(query) prefix = Prefix() prefix.parse_query(query) unwrap = Unwrap() unwrap.parse_query(query) pm = PrimeMeridian() pm.parse_query(query) sc = ShpCabinet() geom_dict = sc.get_geom_dict(key, attr_filter=select_ugid.value) ## unwrap coordinates if requested if unwrap.value: unwrap_geoms(geom_dict, pm.value) dir_path = get_temp_path(nest=True, only_dir=True, wd=env.DIR_OUTPUT) if prefix.value is None: out_name = key else: out_name = prefix.value filename = '{0}.shp'.format(out_name) path = os.path.join(dir_path, filename) path = sc.write(geom_dict, path) path = os.path.split(path)[0] resp = helpers._zip_response_(path, filename=filename.replace('shp', 'zip')) return (resp)
def get_shp(request,key=None): query = helpers.parse_qs(request.META['QUERY_STRING']) select_ugid = SelectUgid() select_ugid.parse_query(query) prefix = Prefix() prefix.parse_query(query) unwrap = Unwrap() unwrap.parse_query(query) pm = PrimeMeridian() pm.parse_query(query) sc = ShpCabinet() geom_dict = sc.get_geom_dict(key,attr_filter=select_ugid.value) ## unwrap coordinates if requested if unwrap.value: unwrap_geoms(geom_dict,pm.value) dir_path = get_temp_path(nest=True,only_dir=True,wd=env.DIR_OUTPUT) if prefix.value is None: out_name = key else: out_name = prefix.value filename = '{0}.shp'.format(out_name) path = os.path.join(dir_path,filename) path = sc.write(geom_dict,path) path = os.path.split(path)[0] resp = helpers._zip_response_(path,filename=filename.replace('shp','zip')) return(resp)
def test_wrap_360_cross_axis(self): row = VectorDimension(value=40,bounds=[38,42]) col = VectorDimension(value=180,bounds=[179,181]) grid = SpatialGridDimension(row=row,col=col) sdim = SpatialDimension(grid=grid,crs=WGS84()) orig_sdim = deepcopy(sdim) sdim.crs.wrap(sdim) self.assertIsInstance(sdim.geom.polygon.value[0,0],MultiPolygon) sdim.crs.unwrap(sdim) self.assertEqual(orig_sdim.geom.polygon.value[0,0].bounds,sdim.geom.polygon.value[0,0].bounds) for target in ['point','polygon']: path = get_temp_path(name=target,suffix='.shp',wd=self._test_dir) sdim.write_fiona(path,target)
def __init__(self,so,mode='raw',prefix='ocg',wd=None,ops=None,add_meta=True,nest=True): self.so = so self.ops = ops self.prefix = prefix if self._create_directory: if wd is None: wd = tempfile.gettempdir() self.wd = get_temp_path(wd=wd,nest=nest,only_dir=True,dir_prefix=prefix) else: self.wd = None self.mode = mode self.add_meta = add_meta if self._ext is None: self.path = self.wd else: self.path = os.path.join(self.wd,prefix+'.'+self._ext)