Agisoft Metashape

Agisoft Metashape => Python and Java API => Topic started by: r0xx on December 23, 2014, 01:03:30 PM

Title: Use the "Convert" command
Post by: r0xx on December 23, 2014, 01:03:30 PM
Hello

I am importing photos into my project using the following code:

Code: [Select]
  doc = PhotoScan.app.document
 
  project_folder = PhotoScan.app.getExistingDirectory("Folder:")
 
  chunk = PhotoScan.app.document.addChunk()
  chunk.label = "1"
 
  my_images = []
  for photo in os.listdir(path_photos):
    if photo.lower().endswith(".jpg") or photo.lower().endswith(".jpeg"):
        my_images.append(path_photos + photo)
        continue
    else:
        continue
 
  chunk.addPhotos(my_images)
  doc.addChunk(chunk)


How can I use the "Convert" command from Reference? Right now I have WGS84 coordinates and I want to transform into EPSG::21781. How would I do that in python?
Title: Re: Use the "Convert" command
Post by: Alexey Pasumansky on December 23, 2014, 02:00:24 PM
Hello r0xx,

In the version 1.1.0 you can do the following to convert camera coordinates from WGS84 to the coordinate system on a different Datum:

Code: [Select]
import math
import PhotoScan

print("Script started")

doc = PhotoScan.app.document
chunk = doc.chunk

new_crs = PhotoScan.CoordinateSystem("EPSG::21781")
new_wkt = new_crs.wkt.split("TOWGS84[")[1].split("]")[0]
dx, dy, dz, rx, ry, rz, s = [float(x) for x in new_wkt.split(",")]

rx = rx * math.pi  / (180. * 3600.)
ry = ry * math.pi  / (180. * 3600.)
rz = rz * math.pi  / (180. * 3600.)
s = 1.0 + s / 1.0E6

R = s * PhotoScan.Matrix([[1, -rz, ry], [rz, 1, -rx], [-ry, rx, 1]])
T = PhotoScan.Matrix([[R[0,0], R[0,1], R[0,2], dx], [R[1,0], R[1,1], R[1,2], dy], [R[2,0], R[2,1], R[2,2], dz], [0, 0, 0, 1]])

for camera in chunk.cameras:

new_loc = new_crs.project(T.inv().mulp(chunk.crs.unproject(camera.reference.location)))
camera.reference.location = new_loc

chunk.crs = new_crs
PhotoScan.app.update()

print("Script finished")

We'll try to implement additional functionality for the final 1.1.0 release for more convenient conversion operation.
Title: Re: Use the "Convert" command
Post by: r0xx on December 23, 2014, 07:13:50 PM
Thanks alot! Your code works great and does exactly what I need...

An easier method would be appreciated but is not necessary at all if there is a workaround I think...
Title: Re: Use the "Convert" command
Post by: Alexey Pasumansky on December 23, 2014, 10:47:16 PM
Hello r0xx,

Nice to hear, that it works for your task.
But in case the original coordiante system is not WGS84, you'll need to calculate toWGS matrix for it as well.

But if both coordinate systems uses the same Datum, you just can use unproject / project combination without calculating additional matrix.
Title: Convert Button Python Equivalent
Post by: IanManning on March 24, 2016, 07:42:58 AM
Hi there,

Looking at processing images from UAVs using a script in PhotoScan Pro 1.2.3. I've been able to get everything working, save a small detail.

Coordinates come off my UAV in WGS84 coordinate system. Using these coordinates as camera positions withReference Photo Alignment, I'm able to produce georeferenced point clouds in the WGS84 GCS. I eventually want to tie down the model to a projected coordinate system using markers.

In the Graphic interface this is a super simple process. Under the reference tab there is a little white and blue spreadsheet icon. When you hover over it, the word "Convert" appears. If you click the button, a dialog box opens that allows you choose a projected coordinate system from a dropdown.

My question is, how can I replicate this simple button click and drop down selection in a Python Script?


Thank you,
Ian
Title: Re: Convert Button Python Equivalent
Post by: Alexey Pasumansky on March 24, 2016, 11:09:42 AM
Hello Ian,

I can propose the following solution:

Code: [Select]
import PhotoScan
chunk = PhotoScan.app.document.chunk #active chunk

new_crs = PhotoScan.CoordinateSystem("EPSG::32633") #just an example

for camera in chunk.cameras:
      camera.reference.location = new_crs.project(chunk.crs.unproject(camera.reference.location))

chunk.crs = new_crs

The script converts coordinates of the camera centers to the new coordinate system and then switches the coordinate system for the active chunk. Note that the conversion is only possible for the coordinate systems that have TOWGS parameters defined.
Title: Re: Convert Button Python Equivalent
Post by: IanManning on March 24, 2016, 02:38:21 PM
Thanks Alexey,

That worked like a treat! Thank you for your help.

Best,
Ian
Title: Re: Use the "Convert" command
Post by: MMNN on May 26, 2016, 11:55:46 AM
If I use it to go from utm32 (EPSG::32632) to ETRS89+DVR90height (EPSG::4098) I keep getting an error:
"error return without exception set"
Title: Re: Use the "Convert" command
Post by: Alexey Pasumansky on May 26, 2016, 02:49:12 PM
Hello MMNN,

Please check the example above, posted in March, I've just merged these two threads together.