parameters: ()
docstring: Main function
docstring_summary: Main function
docstring_tokens: Main function
function: def main():
      """Main function"""
      #usage
      usage = "usage: %prog [options] las_files"
      parser = OptionParser(usage=usage)
      #output filename
      parser.add_option("-o", "--output", dest="name", required=True,
                        help="output file")        
      #ogr format
      parser.add_option("-f", "--format", dest="ogr", default='ESRI Shapefile',
                        metavar="OGR", help="OGR format for output file" \
                        + " [default=%default]")
      #epsg code
      parser.add_option("-e", "--epsg", dest="epsg", default=4326,
                        metavar="EPSG", help="EPSG code of input and output file" \
                        + " [default=%default]")
                        
      #return options and argument
      (options, args) = parser.parse_args()
      #test if args[0] it is set
      if len(args) == 0:
          parser.error("You have to pass the las files to create the tile index")
      
      #create the SpatialReference from epsg code
      outSpatial = osr.SpatialReference()
      outSpatial.ImportFromEPSG(int(options.epsg))
      #create the output driver
      outDriver = ogr.GetDriverByName(options.ogr)
      if outDriver is None:
          print "%s driver not available.\n" % options.ogr
          sys.exit( 1 )
      outputDSN = options.name
      #search if the file already exist
      if os.path.exists(outputDSN):
          outDriver.DeleteDataSource(outputDSN)
      #create output datasource
      outDatasource = outDriver.CreateDataSource(outputDSN)
      if outDatasource is None:
          print "Creation of output file failed.\n"
          sys.exit( 1 )    
      #create output layer
      outLayer = outDatasource.CreateLayer('las2tindex', srs=outSpatial, geom_type=ogr.wkbPolygon)
      if outLayer is None:
          print "Layer creation failed.\n"
          sys.exit( 1 )    
      #output field definition
      outField = ogr.FieldDefn("location", ogr.OFTString)
      outField.SetWidth( 32 )
      if outLayer.CreateField ( outField ) != 0:
          print "Creating 'location' field failed.\n"
          sys.exit( 1 )
      #for each file create the polygon of bounding box
      for i in args:
          # if python liblas it is present use it
          if llas:
              lasfile = liblas.file.File(i,mode='r')
              header = lasfile.header
              minimum = header.get_min()
              maximum = header.get_max()
          # else use lasinfo command
          elif not llas:
              command = "lasinfo %s" % i
              lasout = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE).stdout
              bbox = []
              # for each line of output check min and max values
              for line in lasout.readlines():
                  if re.search(r'Min X Y Z:', line) is not None or \
                  re.search(r'Max X Y Z:', line) is not None:
                      values=line.split(':')[1].lstrip().split()
                      del values[2]
                      bbox.append(values)
                  if len(bbox) == 2:
                      break
              minimum = bbox[0]
              maximum = bbox[1]
          #create the points of polygon
          ld = "%s %s" % (minimum[0],minimum[1])
          lu = "%s %s" % (minimum[0],maximum[1])
          ru = "%s %s" % (maximum[0],maximum[1])
          rd = "%s %s" % (maximum[0],minimum[1])
          # create the feature
          feat = ogr.Feature( outLayer.GetLayerDefn())
          feat.SetField( "location", i )
          wkt = 'POLYGON((%s,%s,%s,%s,%s))' % (ld, lu, ru, rd, ld)
          poly = ogr.CreateGeometryFromWkt(wkt, outSpatial)
          feat.SetGeometry(poly)
          #load the feature into vector layer
          if outLayer.CreateFeature(feat) != 0:
              print "Failed to create feature in vector file.\n"
              sys.exit( 1 )
          feat.Destroy()        
  
      outDatasource = None
      print "%s created" % outputDSN
function_tokens: def main ( ) : #usage usage = "usage: %prog [options] las_files" parser = OptionParser ( usage = usage ) #output filename parser . add_option ( "-o" , "--output" , dest = "name" , required = True , help = "output file" ) #ogr format parser . add_option ( "-f" , "--format" , dest = "ogr" , default = 'ESRI Shapefile' , metavar = "OGR" , help = "OGR format for output file" + " [default=%default]" ) #epsg code parser . add_option ( "-e" , "--epsg" , dest = "epsg" , default = 4326 , metavar = "EPSG" , help = "EPSG code of input and output file" + " [default=%default]" ) #return options and argument ( options , args ) = parser . parse_args ( ) #test if args[0] it is set if len ( args ) == 0 : parser . error ( "You have to pass the las files to create the tile index" ) #create the SpatialReference from epsg code outSpatial = osr . SpatialReference ( ) outSpatial . ImportFromEPSG ( int ( options . epsg ) ) #create the output driver outDriver = ogr . GetDriverByName ( options . ogr ) if outDriver is None : print "%s driver not available.\n" % options . ogr sys . exit ( 1 ) outputDSN = options . name #search if the file already exist if os . path . exists ( outputDSN ) : outDriver . DeleteDataSource ( outputDSN ) #create output datasource outDatasource = outDriver . CreateDataSource ( outputDSN ) if outDatasource is None : print "Creation of output file failed.\n" sys . exit ( 1 ) #create output layer outLayer = outDatasource . CreateLayer ( 'las2tindex' , srs = outSpatial , geom_type = ogr . wkbPolygon ) if outLayer is None : print "Layer creation failed.\n" sys . exit ( 1 ) #output field definition outField = ogr . FieldDefn ( "location" , ogr . OFTString ) outField . SetWidth ( 32 ) if outLayer . CreateField ( outField ) != 0 : print "Creating 'location' field failed.\n" sys . exit ( 1 ) #for each file create the polygon of bounding box for i in args : # if python liblas it is present use it if llas : lasfile = liblas . file . File ( i , mode = 'r' ) header = lasfile . header minimum = header . get_min ( ) maximum = header . get_max ( ) # else use lasinfo command elif not llas : command = "lasinfo %s" % i lasout = subprocess . Popen ( command , shell = True , stdout = subprocess . PIPE ) . stdout bbox = [ ] # for each line of output check min and max values for line in lasout . readlines ( ) : if re . search ( r'Min X Y Z:' , line ) is not None or re . search ( r'Max X Y Z:' , line ) is not None : values = line . split ( ':' ) [ 1 ] . lstrip ( ) . split ( ) del values [ 2 ] bbox . append ( values ) if len ( bbox ) == 2 : break minimum = bbox [ 0 ] maximum = bbox [ 1 ] #create the points of polygon ld = "%s %s" % ( minimum [ 0 ] , minimum [ 1 ] ) lu = "%s %s" % ( minimum [ 0 ] , maximum [ 1 ] ) ru = "%s %s" % ( maximum [ 0 ] , maximum [ 1 ] ) rd = "%s %s" % ( maximum [ 0 ] , minimum [ 1 ] ) # create the feature feat = ogr . Feature ( outLayer . GetLayerDefn ( ) ) feat . SetField ( "location" , i ) wkt = 'POLYGON((%s,%s,%s,%s,%s))' % ( ld , lu , ru , rd , ld ) poly = ogr . CreateGeometryFromWkt ( wkt , outSpatial ) feat . SetGeometry ( poly ) #load the feature into vector layer if outLayer . CreateFeature ( feat ) != 0 : print "Failed to create feature in vector file.\n" sys . exit ( 1 ) feat . Destroy ( ) outDatasource = None print "%s created" % outputDSN