Skip to content

Commit

Permalink
mostly work on new Vector class
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Bierbower authored and Will Bierbower committed Apr 1, 2015
1 parent 5e42fdd commit c0dab75
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 180 deletions.
70 changes: 30 additions & 40 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,45 @@ Installation

.. code::
pip install fauxgeo
pip install fauxgeo
Features
--------

* Raster Class
* TestRaster Class
* RasterFactory Class

* Vector Class
* VectorFactory Class

Usage
-----

The Raster Class

.. code:: python
raster = Raster.from_file('path/to/geotiff')
raster.uri # equals '/path/to/geotiff'
raster.get_band(1) # returns 2d numpy array
raster.get_bands() # returns 3d numpy array
raster.get_nodata() # returns nodata value
raster.shape() # returns 2-tuple (rows, cols)
The TestRaster Class
.. code:: python
import numpy as np
from affine import Affine
import gdal
import numpy as np
from affine import Affine
import gdal
# set arguments
array = np.ones((3, 3))
affine = Affine.identity()
proj = 4326
datatype = gdal.GDT_Float64
nodata_val = -9999.0
# set arguments
array = np.ones((3, 3))
affine = Affine.identity()
proj = 4326
datatype = gdal.GDT_Float64
nodata_val = -9999.0
raster = Raster.from_array(array, affine, proj, datatype, nodata_val)
raster = Raster.from_file('path/to/geotiff')
# uses tempfile to create temporary file
test_raster = TestRaster.from_array(array, affine, proj, datatype, nodata_val)
raster.uri # equals '/path/to/geotiff'
raster.get_band(1) # returns 2d numpy array
raster.get_bands() # returns 3d numpy array
raster.get_nodata() # returns nodata value
raster.shape() # returns 2-tuple (rows, cols)
# same functions as Raster class
raster.get_band(1) # returns 2d numpy array
raster.get_bands() # returns 3d numpy array
raster.get_nodata() # returns nodata value
raster.shape() # returns 2-tuple (rows, cols)
del test_raster # cleans up temporary file on object deletion or program exit
del test_raster # cleans up temporary file on object deletion or program exit
The RasterFactory Class
Expand Down Expand Up @@ -111,18 +101,18 @@ Tests
-----

.. code::
python setup.py test
python setup.py test
Planning
--------

* Add basic visualization functionality
* Add Vector, TestVector, and VectorFactory classes
* Add sample/default arguments for Raster classes to simplify raster creation

Notes
-----

* Look into object deletion depending on scope
*
* Raster Operations
* Reclass
* Overlay - intersection, union, clip
* Dissolve
* Buffer
* Raster_to_Vector
* Slope
* Aspect
63 changes: 59 additions & 4 deletions fauxgeo/raster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import shutil

import gdal
import ogr
import osr
import numpy as np
from affine import Affine
from shapely.geometry import Polygon
import pygeoprocessing as pygeo


Expand Down Expand Up @@ -260,7 +262,18 @@ def get_projection(self):
self._open_dataset()
RasterSRS = osr.SpatialReference()
RasterSRS.ImportFromWkt(self.dataset.GetProjectionRef())
return int(RasterSRS.GetAttrValue("AUTHORITY", 1))
proj = int(RasterSRS.GetAttrValue("AUTHORITY", 1))

RasterSRS = None
self._close_dataset()

return proj

def get_projection_wkt(self):
self._open_dataset()
wkt = self.dataset.GetProjectionRef()
self._close_dataset()
return wkt

def get_geotransform(self):
geotransform = None
Expand All @@ -276,7 +289,34 @@ def get_affine(self):
return Affine.from_gdal(*geotransform)

def get_bbox(self):
pass
return pygeo.geoprocessing.get_bounding_box(self.uri)

def get_aoi(self):
bb = self.get_bbox()
u_x = max(bb[0::2])
l_x = min(bb[0::2])
u_y = max(bb[1::2])
l_y = min(bb[1::2])
return Polygon([(l_x, l_y), (l_x, u_y), (u_x, u_y), (u_x, l_y)])

def get_aoi_as_shapefile(self, uri):
bb = self.get_bbox()
u_x = max(bb[0::2])
l_x = min(bb[0::2])
u_y = max(bb[1::2])
l_y = min(bb[1::2])
aoi = Polygon([(l_x, l_y), (l_x, u_y), (u_x, u_y), (u_x, l_y)])
# wkb = aoi.wkb

# shpDriver = ogr.GetDriverByName("ESRI Shapefile")
# if os.path.exists(uri):
# shpDriver.DeleteDataSource(uri)
# outDataSource = shpDriver.CreateDataSource(uri)
# outLayer = outDataSource.CreateLayer(uri, geom_type=ogr.wkbPolygon)
# featureDefn = outLayer.GetLayerDefn()
# outFeature = ogr.Feature(featureDefn)
# outFeature.SetGeometry(wkb)
# outLayer.CreateFeature(outFeature)

def set_band(self, masked_array):
'''Currently works for rasters with only one band'''
Expand Down Expand Up @@ -381,8 +421,23 @@ def dataset_pixel_op(x, y): return y

def clip(self, aoi_uri):
dataset_out_uri = pygeo.geoprocessing.temporary_filename()
pygeo.geoprocessing.clip_dataset_uri(
self.uri, aoi_uri, dataset_out_uri)
datatype = self.get_datatype()
nodata = self.get_nodata()
pixel_size = self.get_affine().a

pygeo.geoprocessing.vectorize_datasets(
[self.uri],
lambda x: x,
dataset_out_uri,
datatype,
nodata,
pixel_size,
'intersection',
aoi_uri=aoi_uri,
assert_datasets_projected=True, # ?
process_pool=None,
vectorize_op=False)

return Raster.from_tempfile(dataset_out_uri)

def reproject(self, proj, resample_method, pixel_size=None):
Expand Down
33 changes: 13 additions & 20 deletions fauxgeo/raster_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from affine import Affine

from fauxgeo.raster import Raster
from fauxgeo.temp_raster import TempRaster


class RasterFactory(object):
Expand All @@ -29,39 +28,33 @@ def get_metadata(self):
meta['affine'] = self.affine
return meta

def _create_raster(self, array, uri):
if uri is None:
return TempRaster.from_array(
array, self.affine, self.proj, self.datatype, self.nodata_val)
else:
return Raster.from_file(
uri, array, self.affine, self.proj, self.datatype, self.nodata_val)
def _create_raster(self, array):
return Raster.from_array(
array, self.affine, self.proj, self.datatype, self.nodata_val)

def uniform(self, val, uri=None):
def uniform(self, val):
a = np.ones((self.rows, self.cols)) * val
return self._create_raster(a, uri)
return self._create_raster(a)

def alternating(self, val1, val2, uri=None):
def alternating(self, val1, val2):
a = np.ones((self.rows, self.cols)) * val2
a[::2, ::2] = val1
a[1::2, 1::2] = val1
return self._create_raster(a, uri)
return self._create_raster(a)

def random(self, uri=None):
def random(self):
a = np.random.rand(self.rows, self.cols)
return self._create_raster(a, uri)
return self._create_raster(a)

def horizontal_ramp(self, val1, val2, uri=None):
def horizontal_ramp(self, val1, val2):
a = np.zeros((self.rows, self.cols))
col_vals = np.linspace(val1, val2, self.cols)
a[:] = col_vals
return self._create_raster(a, uri)
return self._create_raster(a)

def vertical_ramp(self, val1, val2, uri=None):
def vertical_ramp(self, val1, val2):
a = np.zeros((self.cols, self.rows))
row_vals = np.linspace(val1, val2, self.rows)
a[:] = row_vals
a = a.T
return self._create_raster(a, uri)

# def bell_shape(self, uri=None):
return self._create_raster(a)
69 changes: 0 additions & 69 deletions fauxgeo/temp_raster.py

This file was deleted.

0 comments on commit c0dab75

Please sign in to comment.