How can I convert GeoTIFF to sqlite database in python

18 views Asked by At

I have a GeoTIFF file that i want to extract its latitude and longitude with altitude and store it in a sqlite database, this sqlite database will be later used in a mobile application, For now this is the code that will give me latitude and longitude with altitude

import rasterio

def get_elevation_from_geotiff(file_path, lon, lat):
    dataset = rasterio.open(file_path)
    transform = dataset.transform
    col, row = ~transform * (lon, lat)  # Inverse transform
    col = int(col)
    row = int(row)
    elevation = dataset.read(1)[row, col]
    dataset.close()
    return elevation

def count_coordinates_in_geotiff(file_path):
    dataset = rasterio.open(file_path)
    num_coordinates = dataset.width * dataset.height
    dataset.close()
    return num_coordinates

# Usage example
geotiff_file = '/Users/pannam/Desktop/terrain_map/assets/nepal.tif'
# Biratnagar
longitude = 87.2662
latitude = 26.4840
# //write the latitude and longitude of the place you want to know the altitude of kathmandu
#Kathmandu
# longitude = 85.3240
# latitude = 27.7172

num_coordinates = count_coordinates_in_geotiff(geotiff_file)
print("Number of coordinates in the GeoTIFF file:", num_coordinates)

altitude = get_elevation_from_geotiff(geotiff_file, longitude, latitude)
print('Altitude at ({}, {}): {} meters'.format(longitude, latitude, altitude))
print('Altitude at ({}, {}): {} feet'.format(longitude, latitude, altitude * 3.28084))

this will print out correct altitude

How can i convert this Geotiff to sqlite which has information like longitude, latitude and altitude? Or is this approach even worth it ? My objective is to color code the map based on the height, like a terrain awareness system.

Please take a look at Flood Simulator , i want to do exactly something like this but for elevation

or Terrain map Elevavtion map

0

There are 0 answers