I am working with .net xml file that has information regarding lanes, roads, junctions of a place and it has one attribute named Shape - from this I have to extracted x and y coordinates which are in meters.
Now I should use these two coordinates and find the elevation information from the DGM1(Digital terrain model 1) folder which has bunch of .tif files.
My approach : I was able to extract the x and y coordinates from the .net xml file but the problem is with the transformation of these coordinates to pixel coordinates. When I try to use the transformed pixel coordinates for extracting elevation information I get the error as "raster is out of bounds".
The transformation parameters I used for the conversion of x,y coordinates to pixel coordinates :
- Pixel Width: 1.0
- Rotation 1: 0.0
- Rotation 2: 0.0
- Pixel Height: -1.0
- Easting of Upper-Left Corner: 462000.5
- Northing of Upper-Left Corner: 5548999.5
Dataset Information: Width: 1000 Height: 1000 Number of Bands: 1 Coordinate Reference System (CRS): EPSG:25832 Bounding Box: BoundingBox(left=462000.0, bottom=5548000.0, right=463000.0, top=5549000.0) Data Type: float32
Formula I used for conversion :
For X coordinate:
X offset = X coordinate − Easting of Upper-Left Corner
**For Y coordinate:**
Y offset = Northing of Upper-Left Corner − Y coordinate
X offset = 312.37−462000.5= **−461688.13**
Y offset =5548999.5−1176.31= **5547823.19**
**Pixel X coordinate:**
X pixel = X offset/Pixel Width
**Pixel y coordinate:**
y pixel = y offset/Absolute Pixel Height
X pixel= −461688.13/1.0 = −461688.13
Y pixel= 5547823.19/1.0 = 5547823.19
But the transformed pixel coordinates are out of raster bounds, can someone help.. ?> Not sure where I am doing wrong but I am assuming there is something wrong with the transformation only.
The code I used :
@mzjn -
normal python code to extract the x and y coordinates from .net xml file, after extracting them I'm using the below code for transformation and extraction of elevation information from .tif file.
# Read the TIF file for elevation data
with rasterio.open(tif_file) as src:
# Read elevation data
elev_data = src.read(1) # Assuming single-band elevation data
# Read the TFW file for georeferencing information
with open(tfw_file, 'r') as tfw:
lines = tfw.readlines()
# Extract georeferencing parameters from the TFW file
pixel_width = float(lines[0])
pixel_height = float(lines[3])
x_coord = float(lines[4])
y_coord = float(lines[5])
# Compute the x and y coordinates in the raster
col = int((x - x_coord) / pixel_width)
row = int((y_coord - y) / pixel_height)
# print (col, row)
# Check if the coordinates are within the raster bounds
if 0 <= row < src.height and 0 <= col < src.width:
elevation = elev_data[row, col]
return elevation
return None # Return None if coordinates are not found in any file