3

At my work, I need to convert different kinds of files into Geotiff files. Now I have to convert a banch o KML files. Those came with the coordinates from the corners of the polygon. Using rasterio to convert to GeoTiff, I need to use West, East, South and North coordinates, which is giving me a couple of meters of error from the rigth location. Any advice? My code and the image on Google Earth:

How the coordinates come on KML file:

[['POLYGON ((-68.02573239434068 10.458541609679036'], [' -67.97139569083748 10.447019877002532'], [' -67.9631296429911 10.495261426980813'], [' -68.01557210143345 10.506382845074937'], [' -68.02573239434068 10.458541609679036))']

Code:

  south = np.float64(lat_min)
  north = np.float64(lat_max)
  east = np.float64(lon_min)
  west = np.float64(lon_max)

  with rio.open(png, 'r') as img:
  img = img.read([1])
  img = img.astype('uint16')
  bounds = rio.transform.from_bounds(west, south, east, north, 
  img.shape[1], 
  img.shape[2])
  crs = '+proj=latlong'
  show(img) #shows true color
  with rio.open('{}.tiff'.format(file),'w', 
                 driver='GTiff',
                 count=1,
                 height=img.shape[1],
                 width=img.shape[2],
                 dtype=img.dtype,
                 crs=crs,
                 transform= bounds,
                 ) as dst:
        dst.write(img)

1 Answer 1

0

rio.transform.from_bounds takes in order width, height. You are specifiying in order shape[1] then shape[2]

But in the other hand, in open you are specifying: height=img.shape[1], width=img.shape[2]

I think there lies the issue of your code, you should have written:

 tf = rio.transform.from_bounds(
   west, 
   south, 
   east, 
   north, 
   width=img.shape[2], 
   height=img.shape[1]
)

I also think you should use EPSG:4326 as KML crs, as specified in GDAL: https://gdal.org/drivers/vector/kml.html

7
  • Thanks, it did help on the message that returned, but it dind't fixed the location error. Mar 7 at 16:55
  • This should have modified the location, no ? (not upper left corner, but the resolutions x,y should change, so the displayed length of your raster)
    – remi.braun
    Mar 7 at 19:23
  • You also should set crs="epsg:4326" I think, which is the default for KMLs
    – remi.braun
    Mar 7 at 19:24
  • epsg:4326 isn't the CRS of KML, see the standard ~ docs.ogc.org/is/12-007r2/12-007r2.html Appendix B KML Coordinate Reference System Definition (Normative). The CRS is lon/lat ~ opengis.net/def/crs/OGC/0/LonLat84 NOT the lat/long of EPSG:4326
    – nmtoken
    Mar 8 at 11:43
  • Yes OK, but GDAL uses 4326: gdal.org/drivers/vector/kml.html This code is based on rasterio which is wrapping GDAL, so I think I would stick to 4326...
    – remi.braun
    Mar 8 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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