1

I have a simple polygon shape in a GeoJSON file.

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              11.67022705078125,
              46.81885778879603
            ],
            [
              12.402191162109375,
              46.81885778879603
            ],
            [
              12.402191162109375,
              47.1514353582905
            ],
            [
              11.67022705078125,
              47.1514353582905
            ],
            [
              11.67022705078125,
              46.81885778879603
            ]
          ]
        ]
      }
    }
  ]
}

I want to use this to crop a DEM file from the SRTM data and export the cropped area into a bmp file that can be later sent to a CNC machine. This particular polygon is in the W020N90 area, so I'm using that data as source.

First, I translate the .geojson to a .shp file:

ogr2ogr -f 'ESRI Shapefile' area.shp area.geojson

And then attempt to use it to crop the DEM file and create a bmp output file:

$ gdalwarp -cutline area.shp -crop_to_cutline -ot Byte -of bmp -overwrite W020N90.DEM out.bmp
Creating output file that is 87P x 39L.
Processing W020N90.DEM [1/1] : 0Using internal nodata values (e.g. -9999) for image W020N90.DEM.
Copying nodata values from source W020N90.DEM to destination out.bmp.
...10...20...30...40...50...60...70...80...90...100 - done.

This results in a "small white rectangle" of a bmp which is not what I want. I would expect a larger bmp that has some visible features, as there are valleys and mountain ranges in the selected polygon.

  • 1
    The size of your polygon is Extent: (11.670227, 46.818858) - (12.402191, 47.151435) and the pixel size of DEM is 0.00833333. Size 87 x 39 in out.bmp looks right to me. What did you expect? Notice that source data is 16 bit and BMP supports only 8 bit. Is BMP the only supported format for your CNC? If it is then you must scale the data. But perhaps you need a DEM with better resolution. And you can just save the json and use it directly ` gdalwarp -cutline cutline.json -crop_to_cutline -ot Byte -of bmp -overwrite W020N90.DEM out.bmp` – user30184 Jun 1 at 22:02
  • 1
    area.shp is a rectangle? Have you checked the result e.g. in QGIS, try to info the cells. It can be possible good, just large differences in values result it. E.g. try it with nodata 0. – pnz Jun 1 at 23:27
1

The output image is ruined when the 16 bit source is converted directly with gdalwarp into 8 bit BMP image without scaling the data range. I suggest to make the conversion in two steps.

gdalwarp -cutline cutline.json -crop_to_cutline -of GTiff -overwrite W020N90.DEM out.tif

gdal_translate -of bmp -ot byte -scale out.tif out.bmp

Gdalinfo about the out.tif shows the original data range

 Minimum=821.000, Maximum=3425.000, Mean=2180.355, StdDev=491.529

Gdalinfo about the scaled BMP shows that the full range is now utilized

  Minimum=0.000, Maximum=255.000, Mean=133.643, StdDev=48.317

Without scaling all values in the BMP were 255 because the minimum height inside the cutline was 821.

Read more about scaling from the documentation of gdal_translate https://gdal.org/programs/gdal_translate.html if you do not want to stretch the heigth to full 8 bit scale 0-255. Learn also how to use gdalinfo -stats and importance of deleting the *.aux.xml files if you experiment by creating new files with differents settings but using the same file name. The aux.xml files keep the statistics and if you do not delete the file you will see the old statistics.

Edit

Actually gdalwarp -overwrite deletes the .aux.xml file but in some other workflows the old statistics may remain.

| improve this answer | |
  • Wow, thank you for all of this. gdal is such a beast (in a good way), still getting to grips with it. – Milan Cermak Jun 2 at 20:11

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.