Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8848173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:21:54+00:00 2026-06-14T12:21:54+00:00

I wrote the following function. This function reads *.las point format and create a

  • 0

I wrote the following function. This function reads *.las point format and create a raster grid image via GDAL. With dtype i can choose the format of the raster grid following GDAL description. I used several if…else statement but i wish some suggestion i code in order to save line and in order t have my function more elegant

    if dtype == "GDT_Unknown": # Unknown or unspecified type
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Unknown)
    elif dtype == "GDT_Byte": # Eight bit unsigned integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Byte)
    elif dtype == "GDT_UInt16": # Sixteen bit unsigned integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_UInt16)
    elif dtype == "GDT_Int16": # Sixteen bit signed integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Int16)
    elif dtype == "GDT_UInt32": # Thirty two bit unsigned integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_UInt32)
    elif dtype == "GDT_Int32": # Thirty two bit signed integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Int32)
    elif dtype == "GDT_Float32": # Thirty two bit floating point
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Float32)
    elif dtype == "GDT_Float64": # Sixty four bit floating point
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Float64)
    elif dtype == "GDT_CInt16": # Complex Int16
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CInt16)
    elif dtype == "GDT_CInt32": # Complex Int32
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CInt32)
    elif dtype == "GDT_CFloat32": # Complex Float32
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CFloat32)
    elif dtype == "GDT_CFloat64": # Complex Float64
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CFloat64)

Thanks in advance

def LAS2MAXGrid(inFile,outFile,gridSize=1,dtype="GDT_Float32",nodata=-9999.00,BBOX=None,EPSG=None):
    if BBOX == None:
        X = []
        Y = []
        for p in lasfile.File(inFile,None,'r'):
            X.append(p.x)
            Y.append(p.y)
        xmax, xmin = max(X),min(X)
        ymax, ymin = max(Y), min(Y)
        del X,Y
    else:
        xmax,xmin,ymax,ymin = BBOX[0],BBOX[1],BBOX[2],BBOX[3]
    # number of row and columns
    nx = int(math.ceil(abs(xmax - xmin)/gridSize))
    ny = int(math.ceil(abs(ymax - ymin)/gridSize))
    # Create an array to hold the values
    data = np.zeros((ny, nx))
    # read all points line-by-line
    for p in lasfile.File(inFile,None,'r'):
        # Compute the x and y offsets for where this point would be in the raster
        dx = int((p.x - xmin)/gridSize)
        dy = int((ymax - p.y)/gridSize)
        if data[dy,dx] >= p.z:
            data[dy,dx] = data[dy,dx]
        elif data[dy,dx] < p.z:
            # Add the z value for that pixel
            data[dy,dx] = p.z
    # Replacing values equal than a limit in a numpy array
    np.putmask(data, data == 0.00,nodata)
     # Create gtif
    if dtype == "GDT_Unknown": # Unknown or unspecified type
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Unknown)
    elif dtype == "GDT_Byte": # Eight bit unsigned integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Byte)
    elif dtype == "GDT_UInt16": # Sixteen bit unsigned integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_UInt16)
    elif dtype == "GDT_Int16": # Sixteen bit signed integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Int16)
    elif dtype == "GDT_UInt32": # Thirty two bit unsigned integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_UInt32)
    elif dtype == "GDT_Int32": # Thirty two bit signed integer
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Int32)
    elif dtype == "GDT_Float32": # Thirty two bit floating point
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Float32)
    elif dtype == "GDT_Float64": # Sixty four bit floating point
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_Float64)
    elif dtype == "GDT_CInt16": # Complex Int16
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CInt16)
    elif dtype == "GDT_CInt32": # Complex Int32
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CInt32)
    elif dtype == "GDT_CFloat32": # Complex Float32
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CFloat32)
    elif dtype == "GDT_CFloat64": # Complex Float64
        target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, gdal.GDT_CFloat64)
    # top left x, w-e pixel resolution, rotation, top left y, rotation, n-s pixel resolution
    target_ds.SetGeoTransform((xmin, gridSize, 0,ymax, 0, -gridSize))
    # set the reference info
    if EPSG is None:
        # Source has no projection (needs GDAL >= 1.7.0 to work)
        target_ds.SetProjection('LOCAL_CS["arbitrary"]')
    else:
        proj = osr.SpatialReference()
        proj.ImportFromEPSG(EPSG)
        # Make the target raster have the same projection as the source
        target_ds.SetProjection(proj.ExportToWkt())
    # write the band
    target_ds.GetRasterBand(1).WriteArray(data)
    target_ds.GetRasterBand(1).SetNoDataValue(nodata)
    target_ds = None
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:21:55+00:00Added an answer on June 14, 2026 at 12:21 pm

    You could try something like this:

    dtypes = {
        "GDT_Unknown": gdal.GDT_Unknown,
        "GDT_Byte": gdal.GDT_Byte, 
        # etc
    }
    
    target_ds = gdal.GetDriverByName('GTiff').Create(outFile, nx,ny, 1, dtypes[dtype])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this function to read Las file and save a shapefile. The function
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map
I wrote the following function in haskell, as it will enumerate every integer: integers
I wrote the following: Object.prototype.length = function(){ var count = -1; for(var i in
I want to call the Sleep function on ASM. So I wrote the following:
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
I wrote the following codes. The problem should be the istringstream function. what did
I have been required to write a function that reads the BSDF data format
(VB.NET, .NET 3.5) I wrote the following function to read some text from txt
ran into a problem this morning. I wrote the following lines: var Markup =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.