I have a raster layer that I have clipped using a polygon in qgis. The extent of the raster layer did not change.
How do I update the extent of my raster layer so that it is the minimum bounding box of my clipping?
[GIS] How to change the extent of a clipped raster layer in qgis to be the mbb
clipextentsqgisraster
Related Solutions
cross-posting this solution to this thread AND user3397's other thread
@lpinner was on the right track, by stating that the extent being used is the entire extent of the 'clipto' shapefile. This is an underlying attribute of that shapefile, and doesn't change according to the current feature being processed by a cursor. So, every time you're feeding the extent object into the Clip tool, you're giving it this same overall extent, over and over.
You can manipulate the output by changing the Extent environment setting - this is done by setting the gp.Extent property. In my experience, there are two different ways of doing this:
- the legacy method involved getting and setting the extent as a space-delimited string,
- the newer (9.3+ I think) method involves instantiating an 'Extent' object, and getting and setting its Xmax, YMax, etc properties
Option 1: legacy method
# Set extent property with a space-delimited string
# extents should be in this order: XMin YMin XMax YMax
gp.Extent = "-180 -90 180 90"
Option 2: modern method
# Create Extent object, and set its attributes
ext = gp.CreateObject('Extent')
ext.XMin, ext.YMin, ext.XMax, ext.YMax = -180, -90, 180, 90
gp.Extent = ext
I'm not sure which ArcGIS versions restrict you to which method (off the top of my head, I think you can do both in 9.3+, but you can't do Option 2 in 9.2-).
So, to solve your problem, you need to extract the XMin, YMin, XMax and YMax lat/longs from each polygon, and change gp.Extent every time. I.e.:
while row:
# do your other processing and extract extent co-ords from current polygon
ext = gp.CreateObject('Extent')
ext.XMin, ext.YMin, ext.XMax, ext.YMax = currentXMin, currentYMin, currentXMax, currentYMax
gp.Extent = ext
gp.Clip_management #...continue with Clip operation
A third option, I guess, would be to write each polygon to a new temporary feature class. This temporary FC's Extent property would then be the same as the individual polygon's extent. This would be a slow operation if you wrote these temp files to disk, but you can write them to ArcGIS's 'in_memory' workspace to keep it nippy. I.e.:
# Get the primary key field name for the polygon shapefile
oidField = gp.Describe(clipit).OIDFieldName
while row: # Iterate through each polygon
oid = row.GetValue(oidField) # get the unique id for this polygon
# Use Select tool to extract individual polygon to temporary in_memory file
# use the unique ID as the SQL query to extract this record
gp.Select_management(clipit, 'in_memory/temp', '"%s" = %s' % (oidField, oid))
# get the polygon's extent
ext = gp.Describe('in_memory/temp').Extent
From here, you can either use this in_memory/temp polygon as ClippingGeometry for the raster Clip tool, or you can extract the Extent object's values and feed them into the Clip tool's 'extent' parameters as text.
I'll cut my answer off here, and wait and see if it helps. I can clarify any points some more my referring to my scripts at work - I'm writing this off the top off my head at the moment.
The raster is clipped exactly to the shapefile boundaries.
But remember, a raster is a kind of matrix. It's composed by n rows and m columns, so the extent of it is rectangular.
The black area are pixels without values, so you only have valid pixels in the extent of the shapefile.
If you want to hide null pixels, check this answer How to display NoData of a raster in QGIS.
What have you done is clip out some values. The result is the desired but the meaning is different:
What have you done:
In this case, NULL
pixels are defined by a value equals to 255. You can clip out those pixels setting a max of 254 and clipping out the remains values:
Sure work, even you can set two boundaries to remove some values of the render (min and max). But QGIS has a dedicated section to remove NULL
or No Data
values. Gives you more control about how to handle these values:
Best Answer
To change the extent of raster,
(1) SAGA tools from
Processing Toolbox
can fit the raster extent to the clipped image.Step 1: Reclassify the area outside of the image from zero to nodata
First, activate
Processing Tools | SAGA | Raster Tools | Reclassify values
Options can be set as:
Grid
: your raster fileMethod
: single (default)old value
: 0.000000 (default)new value
: -99999replace no data values
checkboxnew value for no data values
: -99999replace other values
and [Run]
Step 2: Crop to data
Then next tool is
Processing Tools | SAGA | Raster Tools | Crop to data
and [Run]
However, if you can go back to the clipping process (Clipper), you would get an optimized extent, and the above steps are unnecessary.
(2) Clipper
Crop the extent of the target dataset to the extent of the cutline