3

I am using ArcPy to automate the conversion of LAS files to Raster in ArcGIS. I'd like to extract the spatial reference from each LAS file along the way.

Is it possible to do this using ArcPy tools?

I was able to install and import Laspy, but is it possible to convert a Laspy object to an ArcGIS spatial reference object?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Scott4993
  • 81
  • 5
  • One approach would be to extract the WKT spatial reference using liblas (see link) and then convert the WKT to an arcpy spatial reference object. Check out this related post: https://gis.stackexchange.com/q/203392 – Aaron Jan 23 '19 at 14:35
  • Thanks, and Liblas will work with the ArcGIS distribution of Python? I am having trouble importing the Liblas library. – Scott4993 Jan 23 '19 at 15:29
  • I used "pip install" to successfully install Liblas, but I'm having trouble importing it in a python script. I am using Python 2.7.8 with ArcGIS 10.3, is there a compatibility issue? – Scott4993 Jan 23 '19 at 17:09
  • I like to use anaconda to manage the installs. Check out the following: https://developers.arcgis.com/python/guide/install-and-set-up/#Install-using-Anaconda-for-Python-Distribution – Aaron Jan 23 '19 at 17:23
  • Use laspy, it works fine with arcpy and it's pure python so it's easy to install. liblas is no longer maintained. – mikewatt Jan 23 '19 at 17:47
  • Thanks, I am trying Laspy. I was able to install it to my ArcGIS10.3 python directory but I am not able to import any of the modules in the Library (i.e. File, etc) Is there a version requirement for Python – Scott4993 Jan 23 '19 at 18:55
  • Did you install it via pip? And are you sure you installed it to/are trying to import from the correct Python environment? It works on 2.6+ and 3.5+ – mikewatt Jan 23 '19 at 20:34

1 Answers1

5

Laspy isn't going to give you convenient access to the SRS in a form you can easily consume. LAS files can have either WKT or GeoTIFF keys as the coordinate system description. For consumption in Esri tools (and elsewhere), you always want the WKT.

The most convenient way to get the WKT from an LAS file is to use PDAL. The following script will read a filename from standard input and emit the WKT (or ESRI-morphed WKT) for the file to standard output. You should be able to use this to script for a directory of files using the batching tool of your choice.

  1. Install PDAL via Conda

    conda install -c conda-forge pdal python-pdal

  2. Save the following script to fetch-wkt.py or something:

    import subprocess
    import json
    import sys
    filename = sys.argv[1]
    args = ['pdal', 'info', filename, '--metadata']
    p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    ret = p.communicate()
    if (ret[1]):
        # we are in error
        error = ret[1].decode('utf-8')
        sys.stderr.write('%s' % error)
        sys.exit(1)
    j = json.loads(ret[0].decode('utf-8'))
    srs = j['metadata']['srs']
    
    '''compound coordinate system will have horizontal and vertical'''
    comp_cs = srs['compoundwkt']
    horiz_cs = srs['horizontal']
    vert_cs = srs['vertical']
    
    '''Do this if you need it morphed to ESRI'''
    try:
        esri = sys.argv[2]
        import osr
        reference = osr.SpatialReference()
        success = reference.ImportFromWkt(comp_cs)
        if (success):
            print ("unable to import SRS for file '%s'" % filename)
        reference.MorphToESRI()
        esri = reference.ExportToWkt()
        sys.stdout.write('%s' % esri)
    except IndexError:
        sys.stdout.write('%s' % comp_cs)
    
  3. Invoke it on a file

    ESRI-morphed version:

    python fetch-wkt.py myfile.las esri
    

    OGC WKT1 version:

    python fetch-wkt.py myfile.las 
    
Howard Butler
  • 4,013
  • 22
  • 26
  • The PDAL website has examples of how to create rasters in its workshop at https://pdal.io/workshop/exercises/analysis/dtm/dtm.html and https://pdal.io/workshop/exercises/analysis/rasterize/rasterize.html if you're up for skipping Arc altogether too... – Howard Butler Jan 23 '19 at 20:48
  • Thanks I will try this out. Does the script save the WKT to a file or does it output it into the terminal? Also, is LibLAS off the table as far as an option goes? – Scott4993 Jan 24 '19 at 12:09
  • libLAS does not support LAS 1.4 files. It would need a significant update to do so. – Howard Butler Jan 24 '19 at 13:20
  • Thanks. And would it possible to call this PDAL script you gave me embedded within my ArcPy script and use the output in other processes? – Scott4993 Jan 25 '19 at 12:44
  • Yes. You should be able to adapt the code as necessary. – Howard Butler Jan 25 '19 at 16:32
  • When using the conda command you posted before to install, I'm given the error message that the packages could not be found. – Scott4993 Jan 25 '19 at 18:42
  • The esri function requires module osr, which is part of the gdal package. Add that to your environment with conda install gdal – matt wilkie Nov 27 '19 at 21:27