Forum

Author Topic: programmatically get UTM CoordinateSystem for a project  (Read 6058 times)

kcm

  • Newbie
  • *
  • Posts: 12
    • View Profile
programmatically get UTM CoordinateSystem for a project
« on: January 21, 2021, 11:07:37 PM »
Hi,

This must be possible in the python API but I cannot figure out how to automatically obtain the UTM EPSG code to set the correct UTM CRS for a project that is currently in EPSG::4326. I can readily set it  manually in the UI using:
Code: [Select]
UTMcrs = app.getCoordinateSystem(), but cannot find a method the set it to the correct zone automatically.

KCM

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15282
    • View Profile
Re: programmatically get UTM CoordinateSystem for a project
« Reply #1 on: January 21, 2021, 11:19:40 PM »
Hello KCM,

If you know the EPSG code, then use the following definition:
Code: [Select]
UTMcrs = Metashape.CoordinateSystem("EPSG::4326")
Best regards,
Alexey Pasumansky,
Agisoft LLC

kcm

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: programmatically get UTM CoordinateSystem for a project
« Reply #2 on: January 22, 2021, 12:31:32 AM »
Hey Alexey, thank you but that is already what I am doing, is there a Metashape method to find the UTM EPSG code for centroid long/lat of the project? Or do I just need implement it with a custom method. For example:
Code: [Select]
def getUtmEpsg(lon, lat):
    # simplistic, misses norway
   
    lat_band = str(int((lon + 180) / 6 ) + 1)
   
    lat_band if len(lat_band) == 2 else '0' + lat_band
       
    if lat >= 0 and lat <= 84:
       
        epsg_code = '326' + lat_band
       
    elif lat >= -80:
       
        epsg_code = '327' + lat_band
       
    else:
       
        return None
       
    return 'EPSG::' + epsg_code

KCM

kcm

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: programmatically get UTM CoordinateSystem for a project
« Reply #3 on: January 22, 2021, 12:33:09 AM »
(I am assuming there has to be a Metashape method since it returns the correct UTM code as one of the UI choices with getCoordinateSystem(). )

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15282
    • View Profile
Re: programmatically get UTM CoordinateSystem for a project
« Reply #4 on: January 22, 2021, 01:54:17 PM »
Hello KCM,

Currently there's no built-in method to detect UTM zone based on the Lat/Long values, so if you need to do it automatically, then custom function will be required. I haven't checked, if your code works, but if it has any problems, please let me know and I'll try to suggest another approach.

Something like that seems a bit easier:

Code: [Select]
def get_utm_epsg(lon, lat):
    zone = int((long + 180) / 6)
    if lat > 0:
        epsg = 32601 + zone
    else:
        epsg = 32701 + zone
    return Metashape.CoordinateSystem("EPSG::"+epsg)
« Last Edit: January 22, 2021, 02:04:19 PM by Alexey Pasumansky »
Best regards,
Alexey Pasumansky,
Agisoft LLC

kcm

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: programmatically get UTM CoordinateSystem for a project
« Reply #5 on: January 22, 2021, 08:46:37 PM »
Hey Alexey,

Nice concise change, thanks! Your last line needs this correction to convert the epsg int to str:

Code: [Select]
def get_utm_epsg(lon, lat):
    zone = int((long + 180) / 6)
    if lat > 0:
        epsg = 32601 + zone
    else:
        epsg = 32701 + zone
    return Metashape.CoordinateSystem("EPSG::"+str(epsg))

KCM
« Last Edit: January 22, 2021, 09:26:53 PM by kcm »