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 save_shp(Class, target_path, source_instances, utm_zone, alternates=None): rows = [] keys = Class.get_columns() if not alternates: alternates = {} for instance in source_instances: values = [ instance.__dict__.get(k, Class.__dict__.get(k)) for k in keys ] rows.append(values + list(instance.attributes.values())) columns = [alternates.get(k, k) for k in keys] if not source_instances: field_definitions = [(k, ogr.OFTString) for k in columns] else: field_definitions = [] columns += instance.attributes.keys() for k, v in zip(columns, rows[0]): if isinstance(v, int): field_type = ogr.OFTInteger elif isinstance(v, float): field_type = ogr.OFTReal else: field_type = ogr.OFTString field_definitions.append((k, field_type)) geometryIO.save(targetPath=target_path, targetProj4=geometryIO.proj4LL, sourceProj4=utm_zone.proj4, shapelyGeometries=get_geometries(source_instances), fieldPacks=rows, fieldDefinitions=field_definitions) return target_path
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 save_shapefile(target_path, geotable): if 'wkt' in geotable: # Shapefiles expect (x, y) or (longitude, latitude) coordinate order geometries = [wkt.loads(x) for x in geotable['wkt']] else: xys = geotable[['longitude', 'latitude']].values geometries = [Point(xy) for xy in xys] # Collect name_packs name_packs = [] for index, row in geotable.iterrows(): for column_name, column_value in row.iteritems(): if column_name in ('wkt', 'longitude', 'latitude'): continue if isinstance(column_value, float): column_type = OFTReal elif isinstance(column_value, int): column_type = OFTInteger elif hasattr(column_value, 'strip'): column_type = OFTString else: continue name_packs.append((column_name, column_type)) break # Collect field_packs field_packs = [] for index, row in geotable.iterrows(): field_packs.append(tuple(row[x] for x, _ in name_packs)) # Set field_definitions field_definitions, field_name_by_column_name = [], {} for column_name, column_type in name_packs: field_name = get_field_name(column_name) field_name_by_column_name[column_name] = field_name field_definitions.append((field_name, column_type)) # Save geometryIO.save( target_path, geometryIO.proj4LL, geometries, field_packs, field_definitions) if field_name_by_column_name: Series(field_name_by_column_name).to_csv( replace_file_extension(target_path, '-thesaurus.csv')) return target_path
def test_save_raises_exceptions(self): path = self.get_path() # A geometry has fewer attributes than are actually defined with self.assertRaises(GeometryError): save(path, proj4LL, sourceGeometries, [x[1:] for x in fieldPacks], fieldDefinitions) # A geometry has more attributes than are actually defined with self.assertRaises(GeometryError): save(path, proj4LL, sourceGeometries, [x * 2 for x in fieldPacks], fieldDefinitions) # The driverName is unrecognized with self.assertRaises(GeometryError): save(path, proj4LL, sourceGeometries, driverName='')
def test_save_overwrites_existing_targetPath(self): path = self.get_path() for x in range(2): save(path, proj4LL, sourceGeometries)
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])