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
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
-
Thanks, it did help on the message that returned, but it dind't fixed the location error.Maria Paula Graziotto– Maria Paula Graziotto2023年03月07日 16:55:57 +00:00Commented Mar 7, 2023 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– remi.braun2023年03月07日 19:23:48 +00:00Commented Mar 7, 2023 at 19:23
-
You also should set crs="epsg:4326" I think, which is the default for KMLsremi.braun– remi.braun2023年03月07日 19:24:30 +00:00Commented Mar 7, 2023 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:4326nmtoken– nmtoken2023年03月08日 11:43:51 +00:00Commented Mar 8, 2023 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– remi.braun2023年03月08日 12:29:53 +00:00Commented Mar 8, 2023 at 12:29