Changeset 877

Show
Ignore:
Timestamp:
01/03/12 13:25:49 (17 months ago)
Author:
jjr8
Message:

Added Project Raster To Template tool.

Location:
MGET/Branches/Jason/PythonPackage/src/GeoEco
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/Connectivity/CoralReefConnectivity.py

    r875 r877  
    12511251 
    12521252That situation is usually urealistic and undesirable, and when it 
    1253 happens, the simulator will issue a warning describing the problem. If 
    1254 you experience this problem, you can instruct the tool to guess 
    1255 current values using one of the following algorithms: 
     1253happens the simulator will issue a warning describing the problem. If 
     1254you experience this problem you can instruct the tool to guess current 
     1255values using one of the following algorithms: 
    12561256 
    12571257* Del2a - Laplacian interpolation and linear extrapolation. We 
     
    12681268 
    12691269* Del4 - Same as Del2a but instead of the Laplace operator (also 
    1270   called the del^2 operator) it uses the biharmoic operator (also 
     1270  called the del^2 operator) it uses the biharmonic operator (also 
    12711271  called the del^4 operator). May result in more accurate 
    12721272  interpolations, at some cost in speed. 
     
    12851285estimates that are likely to be inaccurate and should only be used 
    12861286when you are willing to accept those inaccuracies because there is no 
    1287 other way to proceed."""), 
     1287other way to proceed. 
     1288 
     1289Thanks to John D'Errico for providing the code that implements the 
     1290mathematical algorithms described here (click `here 
     1291<http://www.mathworks.com/matlabcentral/fileexchange/4551>`_ for more 
     1292information)."""), 
    12881293    arcGISDisplayName=_(u'Method for estimating missing currents values')) 
    12891294 
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/DataManagement/ArcGISRasters.py

    r870 r877  
    874874        # Perform additional validation. 
    875875 
    876         from GeoEco.Types import EnvelopeTypeMetadata 
    877876        left, bottom, right, top = EnvelopeTypeMetadata.ParseFromArcGISString(extent) 
    878877 
     
    926925        # Perform additional validation. 
    927926 
    928         from GeoEco.Types import EnvelopeTypeMetadata 
    929927        left, bottom, right, top = EnvelopeTypeMetadata.ParseFromArcGISString(extent) 
    930928 
     
    17221720 
    17231721    @classmethod 
     1722    def ProjectToTemplate(cls, inputRaster, templateRaster, outputRaster, resamplingTechnique, interpolationMethod=None, maxHoleSize=None, mask=False, overwriteExisting=False): 
     1723        try: 
     1724            # Perform additional validation. 
     1725 
     1726            if interpolationMethod is not None and resamplingTechnique.lower() not in [u'bilinear', u'cubic']: 
     1727                raise ValueError(_(u'Values cannot be interpolated for No Data cells if the NEAREST or MAJORITY resampling technique is used. Please select the BILINEAR or CUBIC resampling technique instead.')) 
     1728 
     1729            # Look up the coordinate system, extent, and cell size of 
     1730            # the template raster. 
     1731 
     1732            gp = GeoprocessorManager.GetWrappedGeoprocessor() 
     1733            d = gp.Describe(templateRaster) 
     1734            coordinateSystem = gp.CreateSpatialReference(d.SpatialReference).split(';')[0] 
     1735            extent = d.Extent 
     1736            cellSize = d.MeanCellWidth 
     1737             
     1738            # Below, we will set the Extent environment variable and 
     1739            # then call ArcGIS's ProjectRaster_management to 
     1740            # simultaneously project and clip the input raster to the 
     1741            # coordinate system, extent, and cell size of the template 
     1742            # raster. Unfortunately ProjectRaster_management will not 
     1743            # necessarily create a raster that has exactly the desired 
     1744            # extent. It may be one cell larger or smaller in any of 
     1745            # the four directions. Different versions of ArcGIS seem 
     1746            # to work differently in this respect. 
     1747            # 
     1748            # To deal with this annoyance, we will expand the extent 
     1749            # in all four directions by 10 cells--guaranteeing that we 
     1750            # have a raster that is larger than the desired 
     1751            # extent--and then use ExtractByMask_sa to obtain a raster 
     1752            # of the desired extent. 
     1753            # 
     1754            # We use 10 cells rather than 1 cell because of a 
     1755            # different problem: if the caller requested that we 
     1756            # interpolate values for No Data cells, the most accurate 
     1757            # values can be obtained if each No Data region is 
     1758            # completely surrounded by cells with data. In the event 
     1759            # that the template raster extent bisects a No Data 
     1760            # region, the 10 cell buffer increases the chance that we 
     1761            # will interpolate that region using cells from both sides 
     1762            # of the extent line: the side that is within the template 
     1763            # extent and also the side that is outside the extent but 
     1764            # within the buffer. 
     1765 
     1766            from GeoEco.DataManagement.Directories import TemporaryDirectory 
     1767            tempDir = TemporaryDirectory() 
     1768 
     1769            [left, bottom, right, top] = EnvelopeTypeMetadata.ParseFromArcGISString(extent) 
     1770            oldExtent = gp.Extent 
     1771            gp.Extent = '%r %r %r %r' % (left - cellSize*10, bottom - cellSize*10, right + cellSize*10, top + cellSize*10) 
     1772 
     1773            try: 
     1774                # If this is ArcGIS 9.3, set the SnapRaster 
     1775                # environment variable to the template raster. 
     1776                 
     1777                if GeoprocessorManager.GetArcGISMajorVersion() > 9 or GeoprocessorManager.GetArcGISMajorVersion() == 9 and GeoprocessorManager.GetArcGISMinorVersion() >= 3: 
     1778                    oldSnapRaster = gp.SnapRaster 
     1779                    gp.SnapRaster = templateRaster 
     1780 
     1781                # Project the raster. If this is ArcGIS 9.3 or later, 
     1782                # the SnapRaster and Extent environment variables will 
     1783                # ensure the projected cells are snapped and clipped 
     1784                # as requested by the caller. If it is 9.2, use the 
     1785                # Registration_Point parameter instead of SnapRaster, 
     1786                # which did not exist in 9.2 (there was something like 
     1787                # it, but it did not work correctly). 
     1788 
     1789                try: 
     1790                    projectedRaster = os.path.join(tempDir.Path, u'projected.img') 
     1791                    if GeoprocessorManager.GetArcGISMajorVersion() > 9 or GeoprocessorManager.GetArcGISMajorVersion() == 9 and GeoprocessorManager.GetArcGISMinorVersion() >= 3: 
     1792                        gp.ProjectRaster_management(inputRaster, projectedRaster, coordinateSystem, resamplingTechnique, cellSize) 
     1793                    else: 
     1794                        gp.ProjectRaster_management(inputRaster, projectedRaster, coordinateSystem, resamplingTechnique, cellSize, None, extent.rsplit(' ', 2)[0]) 
     1795 
     1796                # If this is ArcGIS 9.3, reset the SnapRaster 
     1797                # environment variable to what it was before. 
     1798                 
     1799                finally: 
     1800                    if GeoprocessorManager.GetArcGISMajorVersion() > 9 or GeoprocessorManager.GetArcGISMajorVersion() == 9 and GeoprocessorManager.GetArcGISMinorVersion() >= 3: 
     1801                        gp.SnapRaster = oldSnapRaster 
     1802 
     1803            # Reset the Extent ArcGIS environment variable to what it 
     1804            # was before. 
     1805             
     1806            finally: 
     1807                gp.Extent = oldExtent 
     1808 
     1809            # If the caller requested that we interpolate values for 
     1810            # No Data regions, do it now. 
     1811 
     1812            if interpolationMethod is not None: 
     1813                from GeoEco.SpatialAnalysis.Interpolation import Interpolator 
     1814                infilledRaster = os.path.join(tempDir.Path, u'infilled.img') 
     1815                Interpolator.InpaintArcGISRaster(projectedRaster, infilledRaster, interpolationMethod, maxHoleSize) 
     1816                projectedRaster = infilledRaster 
     1817 
     1818            # We are about to use ExtractByMask_sa to extract a raster 
     1819            # of the desired extent from the projected raster in the 
     1820            # temp directory. But ExtractByMask_sa also has a side 
     1821            # effect that we might not want: it sets cells that are No 
     1822            # Data in the mask to No Data in the output raster. If the 
     1823            # caller did not request that to happen (using the 
     1824            # template raster as the mask), create a constant raster 
     1825            # that has the same coordinate system, extent, and cell 
     1826            # size as the template raster. We'll use it instead. 
     1827 
     1828            if not mask: 
     1829                oldOutputCoordinateSystem = gp.OutputCoordinateSystem 
     1830                gp.OutputCoordinateSystem = coordinateSystem 
     1831                try: 
     1832                    maskRaster = os.path.join(tempDir.Path, u'constant.img') 
     1833                    gp.CreateConstantRaster_sa(maskRaster, 0, 'INTEGER', cellSize, extent) 
     1834                finally: 
     1835                    gp.OutputCoordinateSystem = oldOutputCoordinateSystem 
     1836 
     1837                # For safety, verify that the output raster has the 
     1838                # expected number of columns and rows. 
     1839                 
     1840                d2 = gp.Describe(maskRaster) 
     1841                if d2.Width != d.Width or d2.Height != d.Height: 
     1842                    raise RuntimeError(_(u'Programming error in this tool: the constant raster does not have the same number of rows or columns as the template raster. Please contact the author of this tool for assistance.')) 
     1843 
     1844            else: 
     1845                maskRaster = templateRaster 
     1846 
     1847            # Extract the raster. 
     1848 
     1849            gp.ExtractByMask_sa(projectedRaster, maskRaster, outputRaster) 
     1850 
     1851        except: 
     1852            Logger.LogExceptionAsError() 
     1853            raise 
     1854 
     1855    @classmethod 
    17241856    def ToPolygons(cls, inputRaster, outputFeatureClass, simplify=True, field=None, projectedCoordinateSystem=None, geographicTransformation=None, resamplingTechnique=None, projectedCellSize=None, registrationPoint=None, clippingDataset=None, clippingRectangle=None, mapAlgebraExpression=None, overwriteExisting=False): 
    17251857        cls.__doc__.Obj.ValidateMethodInvocation() 
     
    29283060CopyArgumentMetadata(BinaryRaster.ToArcGISRaster, u'overwriteExisting', ArcGISRaster.ProjectClipAndOrExecuteMapAlgebra, u'overwriteExisting') 
    29293061ArcGISRaster.ProjectClipAndOrExecuteMapAlgebra.__doc__.Obj.GetArgumentByName(u'overwriteExisting').ArcGISCategory = None 
     3062 
     3063# Public method: ArcGISRaster.ProjectToTemplate 
     3064 
     3065AddMethodMetadata(ArcGISRaster.ProjectToTemplate, 
     3066    shortDescription=_(u'Projects a raster to the coordinate system, cell size, and extent of a template raster.'), 
     3067    isExposedToPythonCallers=True, 
     3068    isExposedByCOM=True, 
     3069    isExposedAsArcGISTool=True, 
     3070    arcGISDisplayName=_(u'Project Raster to Template'), 
     3071    arcGISToolCategory=_(u'Spatial Analysis\\Project Raster to Template'), 
     3072    dependencies=[ArcGISDependency(9, 1)]) 
     3073 
     3074CopyArgumentMetadata(ArcGISRaster.Copy, u'cls', ArcGISRaster.ProjectToTemplate, u'cls') 
     3075 
     3076AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'inputRaster', 
     3077    typeMetadata=ArcGISRasterLayerTypeMetadata(mustExist=True), 
     3078    description=_( 
     3079u"""Raster to project to the template raster's coordinate system, cell 
     3080size, and extent."""), 
     3081    arcGISDisplayName=_(u'Raster to project')) 
     3082 
     3083AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'templateRaster', 
     3084    typeMetadata=ArcGISRasterLayerTypeMetadata(mustExist=True, mustBeDifferentThanArguments=[u'inputRaster']), 
     3085    description=_( 
     3086u"""Raster defining the coordinate system, cell size, and extent of 
     3087the output raster."""), 
     3088    arcGISDisplayName=_(u'Template raster')) 
     3089 
     3090AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'outputRaster', 
     3091    typeMetadata=ArcGISRasterTypeMetadata(mustBeDifferentThanArguments=[u'inputRaster', u'templateRaster'], deleteIfParameterIsTrue=u'overwriteExisting', createParentDirectories=True), 
     3092    description=_( 
     3093u"""Output raster. 
     3094 
     3095If this path refers to the file system, missing directories in the 
     3096path will be created if they do not exist."""), 
     3097    direction=u'Output', 
     3098    arcGISDisplayName=_(u'Output raster')) 
     3099 
     3100AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'resamplingTechnique', 
     3101    typeMetadata=UnicodeStringTypeMetadata(makeLowercase=True, allowedValues=[u'BILINEAR', u'CUBIC']), 
     3102    description=_( 
     3103u"""Resampling algorithm to be used to project the input raster to the 
     3104template raster's coordinate system. One of: 
     3105 
     3106* NEAREST - `Nearest neighbor assignment 
     3107  <http://en.wikipedia.org/wiki/Nearest-neighbor_interpolation>`_. 
     3108 
     3109* BILINEAR - `Bilinear interpolation 
     3110  <http://en.wikipedia.org/wiki/Bilinear_interpolation>`_. 
     3111 
     3112* CUBIC - Cubic convolution, also known as `bicubic interpolation 
     3113  <http://en.wikipedia.org/wiki/Bicubic_interpolation>`_. 
     3114 
     3115* MAJORITY - Majority resampling. This method requires ArcGIS 9.3 or 
     3116  later. 
     3117 
     3118The NEAREST and MAJORITY algorithms should be used for categorical 
     3119data, such as a land use classification. It is not recommended that 
     3120NEAREST or MAJORITY be used for continuous data, such as elevation 
     3121surfaces. 
     3122 
     3123The BILINEAR and CUBIC options are most appropriate for continuous 
     3124data. Do not use BILINEAR or CUBIC with categorical data. 
     3125 
     3126The projection is accomplished with the ArcGIS Project Raster tool. 
     3127Please see the documentation for that tool for more information."""), 
     3128    arcGISDisplayName=_(u'Resampling technique')) 
     3129 
     3130AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'interpolationMethod', 
     3131    typeMetadata=UnicodeStringTypeMetadata(makeLowercase=True, allowedValues=[u'Del2a', u'Del2b', u'Del2c', u'Del4', u'Spring'], canBeNone=True), 
     3132    description=_( 
     3133u"""Method to use to guess values for No Data cells. 
     3134 
     3135Use this option to "fill in" clusters of No Data cells with values 
     3136obtained by interpolation and extrapolation. This option is 
     3137appropriate for rasters representing continuous surfaces, e.g. images 
     3138of sea surface temperature in which cloudy pixels contain No Data. It 
     3139uses algorithms based on differential calculus that may provide more 
     3140accurate guesses than traditional ArcGIS approaches, such as computing 
     3141the focal mean of a 3x3 neighborhood. 
     3142 
     3143This option is not appropriate for rasters representing categorical 
     3144data, such as land cover classifications. Therefore, in order to use 
     3145this option, you must select BILINEAR or CUBIC for the Resampling 
     3146Technique parameter. 
     3147 
     3148The available algorithms are: 
     3149 
     3150* Del2a - Laplacian interpolation and linear extrapolation. 
     3151 
     3152* Del2b - Same as Del2a but does not build as large a linear system of 
     3153  equations. May be faster than Del2a at the cost of some accuracy. 
     3154 
     3155* Del2c - Same as Del2a but solves a direct linear system of equations 
     3156  for the No Data values. Faster than both Del2a and Del2b but is the 
     3157  least robust to noise on the boundaries of No Data cells and least 
     3158  able to interpolate accurately for smooth surfaces. 
     3159 
     3160* Del4 - Same as Del2a but instead of the Laplace operator (also 
     3161  called the del^2 operator) it uses the biharmonic operator (also 
     3162  called the del^4 operator). May result in more accurate 
     3163  interpolations, at some cost in speed. 
     3164 
     3165* Spring - Uses a spring metaphor. Assumes springs (with a nominal 
     3166  length of zero) connect each cell with every neighbor (horizontally, 
     3167  vertically and diagonally). Since each cell tries to be like its 
     3168  neighbors, extrapolation is as a constant function where this is 
     3169  consistent with the neighboring nodes. 
     3170 
     3171This option is applied after the input raster has been projected to 
     3172the coordinate system and cell size of the template raster. 
     3173 
     3174Although this tool can fill No Data clusters of any size, you should 
     3175apply common sense when using it. The larger the cluster, the less 
     3176accurate the guessed values will be, especially for rasters that 
     3177represent a noisy surface. 
     3178 
     3179Thanks to John D'Errico for providing the code that implements the 
     3180mathematical algorithms described here (click `here 
     3181<http://www.mathworks.com/matlabcentral/fileexchange/4551>`_ for more 
     3182information)."""), 
     3183    arcGISDisplayName=_(u'Method for interpolating No Data cells'), 
     3184    arcGISCategory=_(u'Interpolation and masking options')) 
     3185 
     3186AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'maxHoleSize', 
     3187    typeMetadata=IntegerTypeMetadata(mustBeGreaterThan=0, canBeNone=True), 
     3188    description=_( 
     3189u"""Maximum size, in cells of the template raster, that a region of 
     31904-connected No Data cells may be for it to be filled in. 
     3191 
     3192Use this option to prevent the filling of large No Data regions (e.g. 
     3193large clouds in remote sensing images) when you are concerned that 
     3194values cannot be accurately guessed for those regions. If this option 
     3195is omitted, all regions will be filled, regardless of size."""), 
     3196    arcGISDisplayName=_(u'Maximum size of No Data regions to interpolate'), 
     3197    arcGISCategory=_(u'Interpolation and masking options')) 
     3198 
     3199AddArgumentMetadata(ArcGISRaster.ProjectToTemplate, u'mask', 
     3200    typeMetadata=BooleanTypeMetadata(), 
     3201    description=_( 
     3202u"""If True, the template raster will be used to mask the input raster 
     3203after it has been projected to the template raster's coordinate system 
     3204and values have been interpolated for No Data cells (if your requested 
     3205that). Cells of the template raster that are No Data will be set to No 
     3206Data in the output raster, even if you requested that values be 
     3207interpolated for No Data cells. This is appropriate in situations 
     3208where the template defines the areas for which you want to retain 
     3209data; for example, when you are analyzing the ocean and you have a 
     3210mask in which ocean cells have data and land cells are set to No Data. 
     3211 
     3212If False, the template raster will only be used to define the 
     3213coordinate system, cell size, and rectangular extent of the output 
     3214raster, and no masking will be done."""), 
     3215    arcGISDisplayName=_(u'Use template raster as mask'), 
     3216    arcGISCategory=_(u'Interpolation and masking options')) 
    29303217 
    29313218# Public method: ArcGISRaster.ToPolygons 
     
    33823669    findAndProcessMethodName=u'FindAndExtractByMask', 
    33833670    findAndProcessMethodArcGISDisplayName=u'Find ArcGIS Rasters and Extract By Mask', 
    3384     findAndProcessMethodShortDescription=_(u'Finds and rasters in an ArcGIS workspace and extracts the cells that correspond to the areas defined by a mask.'), 
     3671    findAndProcessMethodShortDescription=_(u'Finds rasters in an ArcGIS workspace and extracts the cells that correspond to the areas defined by a mask.'), 
    33853672    findMethod=ArcGISRaster.FindAndCreateTable, 
    33863673    findOutputFieldParams=[u'rasterField'], 
     
    34463733    findAndProcessMethodName=u'FindAndProjectClipAndOrExecuteMapAlgebra', 
    34473734    findAndProcessMethodArcGISDisplayName=u'Find ArcGIS Rasters and Project, Clip, and/or Execute Map Algebra', 
    3448     findAndProcessMethodShortDescription=_(u'Finds and rasters in an ArcGIS workspace and projects, clips, and/or perfoms map algebra on them. You must request at least one of these three operations. If you request multiple operations, the tool performs them in the order they are listed.'), 
     3735    findAndProcessMethodShortDescription=_(u'Finds rasters in an ArcGIS workspace and projects, clips, and/or perfoms map algebra on them. You must request at least one of these three operations. If you request multiple operations, the tool performs them in the order they are listed.'), 
    34493736    findMethod=ArcGISRaster.FindAndCreateTable, 
    34503737    findOutputFieldParams=[u'rasterField'], 
  • MGET/Branches/Jason/PythonPackage/src/GeoEco/SpatialAnalysis/Interpolation.py

    r873 r877  
    907907 
    908908Use this tool to guess values for small clusters of No Data cells in 
    909 rasters that represents continuous surfaces, e.g. images of sea 
    910 surface temperature for which some pixels have been obscured by 
    911 scattered clouds. This tool provides several advantages over 
    912 traditional moving-window methods provided by ArcGIS, such as the 
    913 Focal Statistics tool of the ArcGIS Spatial Analyst: 
     909rasters representing continuous surfaces, e.g. images of sea surface 
     910temperature in which cloudy pixels contain No Data. This tool provides 
     911several advantages over traditional moving-window methods provided by 
     912ArcGIS, such as the Focal Statistics tool of the ArcGIS Spatial 
     913Analyst: 
    914914 
    915915* It uses methods based on differential calculus that may provide more 
    916   accurate guesses than traditional approaches (e.g. computing the 
    917   focal mean of a 3x3 neighborhood). 
    918  
    919 * It can fill No Data regions of any size. 
    920  
    921 * It accurately handles rasters representing global images, where the 
    922   east and west edges are connected. 
     916  accurate guesses than traditional approaches, such as computing the 
     917  focal mean of a 3x3 neighborhood. 
     918 
     919* It accurately handles rasters with global longitudinal extent, for 
     920  which the east and west edges are connected. 
     921 
     922* It can fill No Data clusters of any size. 
    923923 
    924924Although this tool can fill No Data clusters of any size, you should 
     
    947947    typeMetadata=ArcGISRasterTypeMetadata(mustBeDifferentThanArguments=[u'inputRaster'], deleteIfParameterIsTrue=u'overwriteExisting', createParentDirectories=True), 
    948948    description=_( 
    949 u"""Output raster. 
    950  
    951 The output raster will be identical to the input raster where the 
     949u"""The output raster will be identical to the input raster where the 
    952950input raster has data. For cells of the input raster that do not have 
    953951data, the output raster will contain values interpolated according to 
     
    976974 
    977975* Del4 - Same as Del2a but instead of the Laplace operator (also 
    978   called the del^2 operator) it uses the biharmoic operator (also 
     976  called the del^2 operator) it uses the biharmonic operator (also 
    979977  called the del^4 operator). May result in more accurate 
    980978  interpolations, at some cost in speed. 
     
    10751073    findAndProcessMethodName=u'FindAndInpaintArcGISRasters', 
    10761074    findAndProcessMethodArcGISDisplayName=u'Find ArcGIS Rasters and Interpolate No Data Cells', 
    1077     findAndProcessMethodShortDescription=_(u'Finds rasters in an ArcGIS workspace and values for the No Data cells.'), 
     1075    findAndProcessMethodShortDescription=_(u'Finds rasters in an ArcGIS workspace and interpolates values for the No Data cells.'), 
    10781076    findMethod=ArcGISRaster.FindAndCreateTable, 
    10791077    findOutputFieldParams=[u'rasterField'],