Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📗Develop a DEM1A parser and PMTiles #495

Closed
hfu opened this issue May 25, 2024 · 8 comments
Closed

📗Develop a DEM1A parser and PMTiles #495

hfu opened this issue May 25, 2024 · 8 comments
Assignees
Labels
adopt UN Geodata Adoptation Center priority/MUST

Comments

@hfu
Copy link
Contributor

hfu commented May 25, 2024

Current progress

image

Current code

require 'nokogiri'
require 'gdal'
require 'zip'

def convert(input, tif_path)
  puts "#{tif_path}"
  doc = Nokogiri::XML(input) {|config| config.huge}
  lower_corner = doc.at_xpath('//gml:lowerCorner').text.split.map(&:to_f)
  upper_corner = doc.at_xpath('//gml:upperCorner').text.split.map(&:to_f)
  high = doc.at_xpath('//gml:GridEnvelope/gml:high').text.split.map(&:to_i)
  start_point = doc.at_xpath('//gml:GridFunction/gml:startPoint').text.split.map(&:to_i)
  width = high[0] + 1
  height = high[1] + 1

  #tuple_list = doc.at_xpath('//gml:tupleList').text.strip.split("\n").map do |line|
  #  type, value = line.split(',')
  #  value.to_f
  #end

  tuple_list = []
  (start_point[1] * width + start_point[0]).times {|i|
    tuple_list << -9999.0
  }
  input.each_line do |line|
    r = line.split(',')
    tuple_list << r[1].to_f if r.size == 2
  end
  (width * height - tuple_list.size).times {|i|
    tuple_list << -9999.0
  }

  #data = Array.new(width * height, -9999)
  #tuple_list.each_with_index do |value, index|
  #  data[index] = value
  #end

  puts "Start Point: #{start_point}"
  puts "Lower Corner: #{lower_corner}"
  puts "Upper Corner: #{upper_corner}"
  puts "Width: #{width}, Height: #{height}"
  puts "Tuple List size: #{tuple_list.size}, pixels: #{width * height}"
  puts

  driver = Gdal::Gdal.get_driver_by_name('GTiff')
  File.delete(tif_path) if File.exist?(tif_path)
  dataset = driver.create(tif_path, width, height, 1, Gdal::Gdalconst::GDT_FLOAT32)

  min_y, min_x = lower_corner
  max_y, max_x = upper_corner
  pixel_width = (max_x - min_x) / width
  pixel_height = (max_y - min_y) / height
  geo_transform = [min_x, pixel_width, 0, max_y, 0, -pixel_height]
  dataset.set_geo_transform(geo_transform)

  srs = Gdal::Osr::SpatialReference.new
  srs.import_from_epsg(6668)
  dataset.set_projection(srs.export_to_wkt)

  band = dataset.get_raster_band(1)

  flattened_data = tuple_list.pack('f*')
  band.write_raster(0, 0, width, height, flattened_data)

  band.set_no_data_value(-9999.0)

  band.flush_cache
  dataset = nil
end

zip_path = "FG-GML-5741-53-DEM1A.zip"

Zip::File.open(zip_path) do |zip_file|
  zip_file.each do |entry|
    src_name = entry.name
    next unless src_name.downcase.end_with?('.xml')
    dst_name = src_name.sub('.xml', '.tif')
    input = entry.get_input_stream.read
    convert(input, dst_name)
  end
end
@hfu hfu self-assigned this May 25, 2024
@hfu hfu changed the title Develop a dem1a parser Develop a DEM1A parser May 25, 2024
@hfu
Copy link
Contributor Author

hfu commented May 26, 2024

New version under development

https://github.com/UNopenGIS/gmldem2tif/blob/main/gmldem2tif.rb

@hfu
Copy link
Contributor Author

hfu commented May 26, 2024

File downloading

  • 5740
  • 5741
  • 5841
  • 5941
  • 5942
  • 6041
  • 5540
  • 5541
  • 5640
  • 5641

@hfu
Copy link
Contributor Author

hfu commented May 26, 2024

DEM1A Terrain Tiles in PMTiles are getting ready. To be published soon!

@hfu
Copy link
Contributor Author

hfu commented May 26, 2024

A version in 2024-05-27 morning

  • a.tif 15GB
  • mbtiles 687MB
  • pmtiles 666MB

Updating a.tif by:

gdal_merge.py -co COMPRESS=LZW -n -9999.0 -o a.tif dst/*.tif

@hfu
Copy link
Contributor Author

hfu commented May 26, 2024

gdal_merge.py -co COMPRESS=LZW -co BIGTIFF=YES -n -9999.0 -o a.tif dst/*.tif 

@hfu
Copy link
Contributor Author

hfu commented May 27, 2024

a.tif created

image

@hfu hfu changed the title Develop a DEM1A parser Develop a DEM1A parser and PMTiles May 28, 2024
@hfu
Copy link
Contributor Author

hfu commented Jun 2, 2024

I am happy to relase 1m GSD terrain tiles sourced from Geospatial Information Authority of Japan (GSI) in Dhaka, because we received the use approval from GSI recently. https://observablehq.com/d/c0a3f48cf7111cd2

image

@hfu hfu added the adopt UN Geodata Adoptation Center label Jun 11, 2024
@hfu hfu changed the title Develop a DEM1A parser and PMTiles 📗Develop a DEM1A parser and PMTiles Jun 11, 2024
@hfu
Copy link
Contributor Author

hfu commented Jun 13, 2024

I am closing this.

I thank @smellman for UNopenGIS/gmldem2tif#1. It would be a new chapter of this story!

@hfu hfu closed this as completed Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adopt UN Geodata Adoptation Center priority/MUST
Projects
None yet
Development

No branches or pull requests

1 participant