pka / georaster

Rust library for accessing geospatial raster images
Apache License 2.0
24 stars 7 forks source link

Read coordinates wanted? #4

Closed mfbehrens99 closed 2 months ago

mfbehrens99 commented 2 months ago

I made a simple implementation to get the value at a specific coordinate:

use std::{fs::File, io::BufReader};

use georaster::geotiff::GeoTiffReader;

const DEM_LOCATION: &str = "data/srtm_germany_dtm.tif";

fn main() {
    let img_file = BufReader::new(File::open(DEM_LOCATION).unwrap());
    let mut tiff = GeoTiffReader::open(img_file).unwrap();

    let (lat, lon) = (47.346721, 11.081273 );

    let (origin_x, origin_y) = tiff.origin().unwrap().into();
    let (pixel_size_x, pixel_size_y) = tiff.pixel_size().unwrap().into();

    let x = ((lon - origin_x) / pixel_size_x) as u32;
    let y = ((lat - origin_y) / pixel_size_y) as u32;

    println!("Origin: {origin_x}, {origin_y}");
    println!("Pixel_size: {pixel_size_x}, {pixel_size_y}");
    println!("Coordinates: {x}, {y}");

    println!("Height in Karlsruhe: {}", tiff.read_pixel(x,y));
}

Is this wanted in this crate?

Would like to contribute!

A little bit of misc like interpolating dem values would also be nice to have (maybe in a geotiff_dem wrapper to not pollute the geotiff or guide people to interpolate on rgb data)

pka commented 2 months ago

A helper function like this definitively fits into georaster. PR very welcome!

mfbehrens99 commented 2 months ago

In order to handle locations I can either:

I think it is okay of a geotiff library to depend on a tiff and a geo library

pka commented 2 months ago

I'm for defining our own struct like:

pub struct Coordinate
{
    pub x: f64,
    pub y: f64,
}

impl From<(f64, f64)> for Coordinate {
    fn from(coords: (f64, f64)) -> Self {
        Coordinate {
            x: coords.0,
            y: coords.1,
        }
    }
}

impl From<[f64; 2]> for Coordinate {
    fn from(coords: [f64; 2]) -> Self {
        Coordinate {
            x: coords[0],
            y: coords[1],
        }
    }
}

This is the same as rust-geo, which I don't want to have as a dependency for now, since it's 2D vector only.