Changeset cb89b0e for src


Ignore:
Timestamp:
Jul 15, 2018, 5:52:06 PM (8 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
debian/bullseye, master
Children:
7b6d348
Parents:
ca02e8f
Message:

loganalyzer now produces a GeoTIFF raster image with correct coordinate system (as show in the meta data). But in QGIS it is not placed at the shown position. So there is something wrong...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/python/bin/gnuboat-log-analyser

    rca02e8f rcb89b0e  
    2121import argparse
    2222import sys
     23import math
    2324import json
    2425import csv
    … …  
    2728import xml.etree.ElementTree as ET
    2829from osgeo import ogr
     30from osgeo import gdal
    2931
    3032from gnuboat.datahandler import loghandler, logdataset
    … …  
    4951                self._output_file.writerow({"X": dataset.latitude(), "Y": dataset.longitude(), "Z": water_depth})
    5052
     53def tile_width(zoomlevel):
     54        tile_size = 256
     55        tiles = math.sqrt(tile_number(zoomlevel))
     56        return {"longitude": 360.0/tiles, "latitude": 180.0/tiles}
     57
     58def tile_number(zoomlevel):
     59        return 2**(zoomlevel*2)
     60
    5161def main(argv):
    5262        input_file_path = None
    5363        output_directory = None
     64        statistics = False
     65        target_nodata_value = 0
     66        target_zoomlevel = 18
    5467
    5568        parser = argparse.ArgumentParser(description='Generates the digital terrain model from gnuboat logfiles')
    … …  
    5871        parser.add_argument('output_directory', metavar='output_directory', type=str,
    5972                                                help='Write the Digital Terrain Model into this directory (output)')
     73        parser.add_argument('-s', '--statistics', dest='statistics', action='store_true',
     74                        help='Print some statistics of the input and result datasets')
    6075        arguments = parser.parse_args(argv)
    6176
    … …  
    7792        else:
    7893                output_directory = arguments.output_directory
     94       
     95        if arguments.statistics is not None:
     96                statistics = arguments.statistics
    7997
    8098        with open(input_file_path, 'r') as input_file:
    8199                logfiles = json.load(input_file)
    82100       
    83         corrected_joined_point_cloud_file_path = "{}/{}".format(output_directory, "corrected_joined_points.csv")
     101        source_data_layer = "corrected_joined_points"
     102        corrected_joined_point_cloud_file_path = "{}/{}.csv".format(output_directory, source_data_layer)
    84103        try:
    85104                output_file = open(corrected_joined_point_cloud_file_path, 'w')
    … …  
    89108        output_file_csv = None
    90109        fields = ['X', 'Y', 'Z']
    91         output_file_csv = csv.DictWriter(output_file, delimiter=' ', quotechar='"',
     110        output_file_csv = csv.DictWriter(output_file, delimiter=',', quotechar='"',
    92111                                                                                fieldnames=fields)
    93112        output_file_csv.writeheader()
    … …  
    118137
    119138        # create ogr file info
    120         ogr_data_layer = "points"
    121         ogr_xml_file = "{}/{}".format(output_directory, "ogr_input_file.xml")
     139        source_xml_file = "{}/{}".format(output_directory, "ogr_input_file.vrt")
    122140        try:
    123                 output_file = open(ogr_xml_file, 'w')
     141                output_file = open(source_xml_file, 'w')
    124142        except IOError as error:
    125143                print('ERROR: Could not open the output file for the ogr data source description: {}'.format(error))
    126144                sys.exit(4)
    127145        xml_data_source = ET.Element("OGRVRTDataSource")
    128         xml_layer = ET.SubElement(xml_data_source, "OGRVRTLayer", {"name": ogr_data_layer})
     146        xml_layer = ET.SubElement(xml_data_source, "OGRVRTLayer", {"name": source_data_layer})
    129147        xml_source = ET.SubElement(xml_layer, "SrcDataSource")
    130148        xml_source.text = corrected_joined_point_cloud_file_path
    131         xml_source_layer = ET.SubElement(xml_layer, "SrcLayer")
    132         xml_source_layer.text = ogr_data_layer
    133149        xml_geometry_type = ET.SubElement(xml_layer, "GeometryType")
    134150        xml_geometry_type.text = "wkbPoint"
    … …  
    142158        # processing data with gdal
    143159        ogr.UseExceptions()
    144         ogr_data_source = ogr.Open(ogr_xml_file)
    145         ogr_layer = ogr_data_source.GetLayer(ogr_data_layer)
     160        source_data = ogr.Open(source_xml_file)
     161        source_layer = source_data.GetLayer(source_data_layer)
     162        source_x_min, source_x_max, source_y_min, source_y_max = source_layer.GetExtent()
     163        if statistics:
     164                print("Source statistics:")
     165                #print("  projection: {}".format(source_layer.GetProjection())
     166                #print("  reference system: {}".format(source_data.GetAttrValue("geocs")))
     167                print("  extents (x_min, y_min, x_max, y_max): {}, {}, {}, {}".format(source_x_min, source_y_min, source_x_max, source_y_max))
     168               
     169        # Create the destination data source
     170        tile_resolution = tile_width(target_zoomlevel)
     171        target_x_res = int(256 / tile_resolution["longitude"] * (source_x_max - source_x_min))
     172        target_y_res = int(256 / tile_resolution["latitude"] * (source_y_max - source_y_min))
     173        print("target x res {}".format(target_x_res))
     174        print("target y res {}".format(target_y_res))
     175        #target_data = gdal.GetDriverByName('MEM').Create('', target_x_res, target_y_res, gdal.GDT_Byte)
     176        #target_data.SetGeoTransform((source_x_min, pixel_size, 0, source_y_max, 0, -pixel_size))
     177        #band = target_ds.GetRasterBand(1)
     178        #band.SetNoDataValue(NoData_value)
     179       
     180        # create the grid data
     181        grid_options = gdal.GridOptions(format = "GTiff", width = target_x_res, height = target_y_res, outputSRS = "WGS84", noData = target_nodata_value, layers = source_data_layer)
     182        #gdal.Grid("/tmp/test.tif", source_xml_file)
     183        gdal.Grid("/tmp/test.tif", source_xml_file, options = grid_options)
    146184
    147185        # creating pipeline
Note: See TracChangeset for help on using the changeset viewer.