def Show(TheDataset, width=800, height=800): # IF a file path is specified, load the dataset if (type(TheDataset) is str): filename, file_extension = os.path.splitext(TheDataset) file_extension = file_extension.lower() if (file_extension == ".shp"): TheDataset = SpaVectors.Load( TheDataset) # load the contents of the layer else: TheDataset = SpaRasters.Load(TheDataset) # Create the view TheView = SpaView(width, height) # Set the bounds of the view to match the dataset TheBounds = TheDataset.GetBounds() TheView.SetBounds(TheBounds) # Render the dataset into the view if (isinstance(TheDataset, SpaRasters.SpaDatasetRaster)): TheView.RenderRaster(TheDataset) elif (isinstance(TheDataset, SpaVectors.SpaDatasetVector)): TheView.RenderVectors(TheDataset) TheView.Show()
def GetInput(InputFile): """ Return an object that can be used for transforming. This is typically a layer object @Protected Parameters: InputFile: File to be used in transform (vector or raster file) Returns: a SpaDatasetRaster or SpaDatasetVector object """ #print(type(InputFile)) if (isinstance(InputFile, str)): Extension = os.path.splitext(InputFile)[1] Extension = Extension.lower() if (Extension == ".shp"): FilePath = InputFile InputFile = SpaVectors.SpaDatasetVector() InputFile.Load(FilePath) else: FilePath = InputFile InputFile = SpaRasters.SpaDatasetRaster() InputFile.Load(FilePath) return (InputFile)
def GetViewForDataset(TheDataset, Width, Height, TheGeographicBounds=(-180, -90, 180, 90)): """ Example function for rendering spatial datasets into a SpaView. This function will create a view of the specified dimensions, render a light blue background and then render the dataset into the view. The view is returned for the user to futher customize, save, or display the contents of the view """ TheView = SpaView.SpaView(Width, Height) TheBounds = TheDataset.GetBounds() TheView.SetBounds(TheBounds) # render the ocean and a neat line around the spatial data TheGeographicBounds = GetBoundingPolygon(TheGeographicBounds) TheGeographicBounds = SpaDensify.Densify(TheGeographicBounds, 5) TheProjectedBounds = SpaReferencing.Transform(TheGeographicBounds, 4326, TheDataset.GetCRS()) TheView.SetFillColor((210, 220, 250)) TheView.RenderRefGeometry(TheProjectedBounds) # Render the spatial data TheLayer = SpaVectors.SpaLayerVector() TheLayer.SetDataset(TheDataset) TheLayer.Render(TheView, RandomColors=True) return (TheView)
def Densify(self, TheDataset, Amount): NewDataset = SpaVectors.SpaDatasetVector() NewDataset.CopyMetadata(TheDataset) NewDataset.SetType(None) NumFeatures = TheDataset.GetNumFeatures() FeatureIndex = 0 while ( FeatureIndex < NumFeatures ): # interate through all the features finding the intersection with the geometry TheGeometry = TheDataset.TheGeometries[FeatureIndex] TheGeometry = self.DensifyGeometry(TheGeometry, Amount) NewDataset.AddFeature(TheGeometry, TheDataset.TheAttributes[FeatureIndex]) FeatureIndex += 1 return (NewDataset)
Zoning_Bay = "../Data/HumboldtCounty/Zoning_Bay.shp" NASABlueMarble = "../Data/NASA/BlueMarbleNG-TB_2004-12-01_rgb_1440x720.TIFF" OutputFolderPath = "../Temp/" ######################################################################### AlbersEqualArea_Parameters = { "datum": "WGS84", "proj": "aea", "lat_1": 40, "lat_2": 60 } if (True): CountriesDataset = SpaVectors.SpaDatasetVector() #create a new layer CountriesDataset.Load(CountriesFilePath) # load the contents of the layer # Need to make sure each line segment has enough points to look good after being projected (i.e. the may curve) CountriesDataset = SpaDensify.Densify(CountriesDataset, 5) # Clip the bounds of the geographic data to be within the possible bounds of the projection WestCoastDataset = SpaVectors.Clip( CountriesDataset, -156, -90, -90, 90) # the zone is 6 degrees wide but we can go wider # WGS 84 / UTM zone 10N Dataset_UTMZone10North = SpaReferencing.Transform(WestCoastDataset, 32610) SpaView.Show(Dataset_UTMZone10North) # Project from UTM to WGS 84
CitiesFilePath = "../Data/NaturalEarth/ne_110m_populated_places_simple.shp" OutputFolderPath = "../Temp/" RasterFilePath = "../Data/MtStHelens/Mt St Helens Post Eruption DEM Int16.tif" #RasterFilePath2="../Data/MtStHelens/Mt St Helens PreEruption DEM Float32.tif" Path1 = "../Data/MtStHelens/Mt St Helens PreEruption DEM Float32.tif" #Path2="../Data/MtStHelens/Mt St Helens Post Eruption DEM.tif" ############################################################################ # SpaView Tests ############################################################################ # Create a new shapefile TheDataset1 = SpaVectors.SpaDatasetVector() #create a new layer # add a square geometry in at 0,0 TheGeometry = shapely.geometry.Polygon([(-10, 10), (10, 10), (10, -10), (-10, -10), (-10, 10)]) TheDataset1.AddFeature(TheGeometry) # Save the result TheDataset1.Save(OutputFolderPath + "Inside.shp") ############################################################################ # Show a vector shapefile TheDataset2 = SpaVectors.SpaDatasetVector() #create a new layer # add a square geometry in at 0,0 TheGeometry = shapely.geometry.Polygon([(-20, 0), (0, 0), (0, -20), (-20, -20),
from SpaPy import SpaReferencing ############################################################################ # Globals ############################################################################ CountriesFilePath="../Data/NaturalEarth/ne_110m_admin_0_countries.shp" OutputFolderPath="../Temp/" ############################################################################ # STLayerVector functions ############################################################################ ######################################################################### # Load the dataset into SpaDatasetVector object. TheDataset=SpaVectors.SpaDatasetVector() #create a new layer TheDataset.Load(CountriesFilePath) # load the contents of the layer ######################################################################### # Plotting operations if (True): ThePlotter=SpaPlot.SpaPlot() ThePlotter.Plot(TheDataset) ThePlotter.Show()
def Transform(self, TheObject): """ Handles projecting a wide variety of types of data This function will be called recursively for datasets and shapely geometries until coordiantes are reached and then ProjectCoordinateFromGeographic() will be called. Shapely is very picky about the contents of geometries so anything that is not considered valid is not added to the result. Parameters: TheObject: Vector object to be projected Return: none """ Result = None if ( isinstance(TheObject, SpaVectors.SpaDatasetVector) ): # have a layer, make a duplicate and then project all the contents of the layer NewLayer = SpaVectors.SpaDatasetVector() NewLayer.CopyMetadata(TheObject) NewLayer.SetType(None) NumFeatures = TheObject.GetNumFeatures() FeatureIndex = 0 while ( FeatureIndex < NumFeatures ): # interate through all the features finding the intersection with the geometry TheGeometry = TheObject.TheGeometries[FeatureIndex] TheGeometry = self.Transform(TheGeometry) if (TheGeometry != None): NewLayer.AddFeature(TheGeometry, TheObject.TheAttributes[FeatureIndex]) FeatureIndex += 1 #TheCRS=self.GetProjParametersFromSettings() NewLayer.SetCRS(self.ToCRS) Result = NewLayer elif (isinstance(TheObject, shapely.geometry.MultiPolygon)): # have a polygon, ThePolygons = [] for ThePolygon in TheObject: NewPolygon = self.Transform( ThePolygon) # deal with interior polys later if (NewPolygon != None): ThePolygons.append(NewPolygon) if (len(ThePolygons) > 0): Result = shapely.geometry.MultiPolygon(ThePolygons) elif (isinstance(TheObject, shapely.geometry.MultiLineString) ): # have an array of line strings , TheLineStrings = [] for TheLineString in TheObject: NewLineString = self.Transform( TheLineString) # deal with interior polys later if (NewLineString != None): TheLineStrings.append(NewLineString) if (len(TheLineStrings) > 0): Result = shapely.geometry.MultiLineString(TheLineStrings) elif (isinstance(TheObject, shapely.geometry.Polygon)): # have a polygon, TheCoordinates = self.Transform( TheObject.exterior.coords) # deal with interior polys later if (TheCoordinates != None): if (len(TheCoordinates) >= 3): # polygon must have at least 3 coordinates Result = shapely.geometry.Polygon(TheCoordinates) elif (isinstance(TheObject, shapely.geometry.LineString)): # have a polygon, TheCoordinates = self.Transform( TheObject.coords) # deal with interior polys later if (len(TheCoordinates) >= 2): # polygon must have at least 3 coordinates Result = LineString(TheCoordinates) elif (isinstance(TheObject, shapely.geometry.Point)): # have a polygon, TheCoordinates = self.Transform( TheObject.coords) # deal with interior polys later Result = shapely.geometry.Point(TheCoordinates) elif (isinstance(TheObject, shapely.coords.CoordinateSequence) ): # have an shapely coordinate sequence Result = [] for TheEntry in TheObject: Coordinate2 = self.Transform(TheEntry) if (Coordinate2 != None): Easting2 = Coordinate2[0] Northing2 = Coordinate2[1] if ((math.isnan(Easting2) == False) and (math.isnan(Northing2) == False) and (Easting2 != 1e+30) and (Northing2 != 1e+30) and (math.isfinite(Northing2)) and (math.isfinite(Easting2))): Result.append((Easting2, Northing2)) elif (isinstance(TheObject, SpaRasters.SpaDatasetRaster)): # have a raster raise ("Sorry, you'll need to call ProjectRaster()") # #raise Exception("Sorry, SpPy does not yet support projecting raster datasets") else: # should be two coordinate values if (TheObject != None): # and (self.TheProjection!=None): Coordinate = TheObject if (len(Coordinate) > 0): X = Coordinate[0] if (isinstance( X, (list, tuple, set))): # have an array of coordinate pairs Result = [] for TheCoordinate in Coordinate: TheCoordinate = self.TransformCoordinate( TheCoordinate[0], TheCoordinate[1]) Result.append(TheCoordinate) else: # Must be a single coordinate Y = Coordinate[1] Result = self.TransformCoordinate(X, Y) return (Result)
from SpaPy import SpaDensify # Paths to files CountriesFilePath = "../Data/NaturalEarth/ne_110m_admin_0_countries.shp" #CountriesFilePath="C:/Temp/celltowsp.shp" OutputFolderPath = "../Temp/" ############################################################################ # STLayerVector functions ############################################################################ ######################################################################### # Informational and then save a clone TheDataset = SpaVectors.SpaDatasetVector() #create a new layer TheDataset.Load(CountriesFilePath) # load the contents of the layer print("Type: " + format(TheDataset.GetType())) # get the type of data in the dataset print("CRS: " + format(TheDataset.GetCRS())) # return the corodinate reference system print("NumFeatures: " + format( TheDataset.GetNumFeatures())) # get the number of features in the dataset print("Bounds: " + format(TheDataset.GetBounds())) # get the spatial bounds of the features
TheDataset.GetCRS()) TheView.SetFillColor((210, 220, 250)) TheView.RenderRefGeometry(TheProjectedBounds) # Render the spatial data TheLayer = SpaVectors.SpaLayerVector() TheLayer.SetDataset(TheDataset) TheLayer.Render(TheView, RandomColors=True) return (TheView) ######################################################################### # Load the countries dataset TheDataset = SpaVectors.SpaDatasetVector() #create a new layer TheDataset.Load(CountriesFilePath) # load the contents of the layer ######################################################################### # Render a shapefile to a view # Simple example to render an existing dataset with a bounding box if (True): # Create an 800 x 400 pixel view TheView = SpaView.SpaView(800, 400) # Move the view to match the bounds of the dataset TheBounds = TheDataset.GetBounds() TheView.SetBounds(TheBounds) # Add a neat line