TransWikia.com

Change projection for LAS file

Geographic Information Systems Asked on August 12, 2021

I have read a LAS file. If I import it in Global Mapper it says the projection, however when I read it with laspy (header.proj_id_1) there is no information associated.
I have edited the coordinates and updated the X,Y,Z values to new values (according to this) for the LAS file and now I would like to give a new projection after updating the coordinates.
What function do I use?

enter image description here

One Answer

I found a minute to dig into this. We don't quite know what your specific file contains, but here's a quick and dirty way to update the GeoKeyDirectoryTag VLR (page 11) using values as described in the geotiff spec.

This does not currently account for the storage of individual projection parameters in a separate GeoDoubleParamsTag or GeoAsciiParamsTag record. I don't have any example data on hand that contains those params, so if you need help extending this I might need you to post something for me.

This updates the file in place, so be warned.

import laspy

src_path = r"C:exampletest.las"

# see here for possible values:
# http://geotiff.maptools.org/spec/geotiff2.7.html
replace_keys = {
    3072: 26912,  # set ProjectedCSTypeGeoKey to NAD83 / UTM 12N
    3076: 9001,   # set ProjLinearUnitsGeoKey to meters
    4099: 9001    # set VerticalUnitsGeoKey to meters
}


def update_geo_keys(path, lookup):
    with laspy.file.File(path, mode='rw') as f:
        for i, vlr in enumerate(f.header.vlrs):
            # search for the GeoKeyDirectoryTag record
            if vlr.record_id != 34735:
                continue

            modified_body = []
            j = 0

            # loop over geo key entries, stored in groups of 4 values
            while j < len(vlr.parsed_body):
                key_id, tag_loc, count, value = vlr.parsed_body[j:j+4]

                if j > 0 and tag_loc != 0:
                    raise NotImplementedError('modification of projection '
                                              'parameters not implemented')

                # update the value for this key ID, if necessary
                try:
                    value = lookup[key_id]
                except KeyError:
                    pass

                # maintain a new list of values to be written
                modified_body.extend((key_id, tag_loc, count, value))
                j += 4

            # update the vlr with our new values, quit searching
            vlr.parsed_body = modified_body
            break
        else:
            raise ValueError('no GeoKeyDirectoryTag found in file')

        f.header.save_vlrs()


update_geo_keys(src_path, replace_keys)

Correct answer by mikewatt on August 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP