def test_save_and_load_work(self): path = self.get_path('.shp.zip') save(path, proj4LL, sourceGeometries) targetProj4, targetGeometries = load(path)[:2] self.assert_('+proj=longlat' in targetProj4) for sourceGeometry, targetGeometry in zip(sourceGeometries, targetGeometries): self.assert_(sourceGeometry.equals(targetGeometry))
def load(Class, path): if not exists(path): raise IOError if path.endswith('.zip'): import geometryIO from crosscompute_table import pandas as pd [ proj4, geometries, field_packs, field_definitions, ] = geometryIO.load(path) # Convert to (longitude, latitude) normalize_geometry = geometryIO.get_transformGeometry( proj4 or geometryIO.proj4LL) geometries = [normalize_geometry(x) for x in geometries] # Convert to (latitude, longitude) geometries = transform_geometries(geometries, drop_z) geometries = transform_geometries(geometries, flip_xy) # Generate table table = pd.DataFrame( field_packs, columns=[x[0] for x in field_definitions]) table['WKT'] = [x.wkt for x in geometries] else: table = super(GeotableType, Class).load(path) # TODO: Consider whether there is a better way to do this table.interpret = create_bound_method(_interpret, table) return table
def _assert_proj_match(self, shp, csv): """Ensure that the projections match before continuing""" # Use fiona to open the shapefile as this includes the projection type shapefile = fiona.open(shp) # read the first line of the csv to get the projection csv_proj = open(csv).readline() # Parse the Projection to a dictionary csv_proj = csv_proj.split('PROJ.4')[1] pairs = [x.split('=') for x in csv_proj.split(' +') if x != '' and '=' in x] csv_proj = {x[0]:x[1] for x in pairs} # Iterate through and ensure all values match in both projection dicts for key in csv_proj.keys(): if key in csv_proj and key in shapefile.crs: try: assert(str(csv_proj[key]) == str(shapefile.crs[key])) except: logger.error("csv and shp Projections Don't Match") raise AssertionError("csv and shapefile Projections Don't Match") # Save the state of the projection # self.proj = shapefile.crs['proj'] # Determine the type of distance measure based on the input projections # measure = 'euclidean' if self.proj == 'utm' else 'haversine' from geometryIO import load proj4 = load(shp)[0] self.measure = 'haversine' if 'longlat' in proj4 else 'euclidean'
def load(Class, geotable_path): lonlat_geometries = geometryIO.load(geotable_path, targetProj4=geometryIO.proj4LL)[1] lonlat_point = GeometryCollection(lonlat_geometries).centroid longitude, latitude = lonlat_point.x, lonlat_point.y zone_number, zone_letter = utm.from_latlon(latitude, longitude)[-2:] return Class(zone_number, zone_letter)
def load(Class, source_path, utm_zone, defaults=None, alternates=None): instances = [] if not defaults: defaults = {} if not alternates: alternates = {} geometries, field_packs, field_definitions = geometryIO.load( source_path, targetProj4=utm_zone.proj4)[1:] for index, ( geometry, field_pack, ) in enumerate(zip(geometries, field_packs)): d = {} for field_value, ( field_name, field_type, ) in zip(field_pack, field_definitions): d[field_name] = field_value instance = Class(id=d.pop('id', index), geometry=geometry, attributes=d) for k, v in defaults.items(): setattr(instance, k, d.pop(k, d.pop(alternates.get(k), v))) instances.append(instance) return instances
def load_geotable(path): if path.endswith('.csv'): table = read_csv(path) elif path.endswith('.zip'): proj4, geometries, fields, definitions = geometryIO.load(path) normalize_geometry = geometryIO.get_transformGeometry(proj4) geometries = [normalize_geometry(x) for x in geometries] table = DataFrame(fields, columns=[x[0] for x in definitions]) table['WKT'] = [x.wkt for x in geometries] else: raise UnsupportedFormat('cannot load geotable (%s)' % path) return table
def test_save_and_load_attributes_work(self): fieldPacks = [( # 'Спасибо'.decode('utf-8'), datetime.datetime(2000, 1, 1), )] fieldDefinitions = [ # ('String', OFTString), ('Date', OFTDate), ] path = self.get_path() save(path, proj4LL, sourceGeometries, fieldPacks, fieldDefinitions) for sourceField, targetField in zip(fieldPacks[0], load(path)[2][0]): self.assertEqual(sourceField, targetField)
def load_geotable(path): if path.endswith('.csv'): table = read_csv(path) elif path.endswith('.zip'): proj4, geometries, fields, definitions = geometryIO.load(path) normalize_geometry = geometryIO.get_transformGeometry(proj4) # Convert to (longitude, latitude) geometries = [normalize_geometry(x) for x in geometries] # Convert to (latitude, longitude) geometries = transform_geometries(geometries, flip_xy) table = DataFrame(fields, columns=[x[0] for x in definitions]) table['WKT'] = [x.wkt for x in geometries] else: raise UnsupportedFormat('cannot load geotable (%s)' % path) return table
def __init__(self, shp, csv, **kwargs): self.shp_p, self.csv_p = shp, csv self.priority_metric = kwargs['prioritize'] if 'prioritize' in kwargs else 'population' # logger.info('Asserting Input Projections Match') # self._assert_proj_match(shp, csv) from geometryIO import load proj4 = load(shp)[0] self.measure = 'haversine' if 'longlat' in proj4 else 'euclidean' # Load in and align input data logger.info('Aligning Network Nodes With Input Metrics') self._network, self._metrics = prep_data( nx.read_shp(shp), # pd.read_csv(csv, header=1), pd.read_csv(csv), loc_tol = self.TOL) logger.info('Computing Pairwise Distances') self.distance_matrix = self._distance_matrix() if len(self.distance_matrix[(self.distance_matrix > 0) & (self.distance_matrix < self.TOL)]) > 0: logger.error("""Dataset Contains Edges, Less Than {tolerance} Meters! This can result in incorrect alignment of metrics and network, where fake nodes are incorrectly assigned metrics. This error is resolved by buffering your input data.""".format(tolerance=self.TOL)) # Set the edge weight to the distance between those nodes self._weight_edges() logger.info('Directing Network Away From Roots') # Transform edges to a rooted graph self.direct_network() # Assert that the netork is a tree self.assert_is_tree() # Build list of fake nodes self.fake_nodes = self.fakes(self.metrics.index) #Fillna values with Zero self._metrics = self.metrics.fillna(0)
def test_load_raises_exceptions(self): # The format is unrecognized path = self.get_path('') with self.assertRaises(GeometryError): load(path)
def test_load_with_targetProj4_works(self): path = self.get_path() save(path, proj4LL, sourceGeometries) self.assert_('+proj=longlat' not in load(path, targetProj4=proj4SM)[0])