1

Looking for an open source tool to grow/shrink raster edge by n pixels. I noticed Grass has r.grow but hoping there might be a way to do it with GDAL, PostGIS or python library.

More information: I want to shrink it around the edge where I have data not the entire rectangular region. Consider this raster: enter image description here

  • Check the extents and pixel size of the original image with gdalinfo, calculate the desired xmin ymin xmax ymax values feed them to -te (target extents) parameter of gdalwarp gdal.org/gdalwarp.html. For making all that automatically you should write a script. – user30184 Mar 23 '16 at 9:04
  • Thanks but I think that will warp all the data, I just want to remove pixels along the edge where data exists. I've added an example. – Ben Mar 25 '16 at 6:56
  • If input srs and output srs are the same the pixels are just copied. But our image clarifies your question and adjusting the extents does not help you. – user30184 Mar 25 '16 at 13:09
1

So I ended up making a GRASS script to mitigate edge effect from course imagery such as Landsat 8. Script is as follows. I know each step could be simplified but have it in steps helps understand what is going on.

Basically it does a neighbourhood count. It uses the output to discover the edge of the imagery. These gaps are given a null value. The null value is filled in with median value from 5x5 neighbourhood. The output is then masked to the original image.

See input & output side by side.

Input

Output

run_command("r.neighbors",
            overwrite = True,
            input = "in@PERMANENT",
            output = "count",
            method = "count",
            size = 5)

run_command("r.mapcalc",
            overwrite = True,
            expression = "step1=if(count@PERMANENT<25  &  count@PERMANENT>0,0,1)")

run_command("r.mapcalc",
            overwrite = True,
            expression = "step2=if( step1@PERMANENT==0,null(),1)")

run_command("r.mapcalc",
            overwrite = True,
            expression = "step3=if( count@PERMANENT ==0,0,1)* step2@PERMANENT")

run_command("r.mapcalc",
            overwrite = True,
            expression = "step4=if(isnull(in@PERMANENT )==1,0,in@PERMANENT)")

run_command("r.mapcalc",
            overwrite = True,
            expression = "step5=step4@PERMANENT * step3@PERMANENT")

run_command("r.neighbors",
            overwrite = True,
            input = "step5@PERMANENT",
            output = "step6",
            method = "median",
            size = 5)

run_command("r.mapcalc",
            overwrite = True,
            expression = "step7=if(isnull(step6@PERMANENT )==1, step6@PERMANENT, in@PERMANENT )")

run_command("r.mapcalc",
            overwrite = True,
            expression = "step8=if(isnull(in)==0,step7,null())")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.