# Forum

### Author Topic: Align coordinate system to bounding box  (Read 9427 times)

#### ThomasVD

• Full Member
• Posts: 107
##### Align coordinate system to bounding box
« on: January 24, 2015, 03:12:22 PM »
Hi everyone,

I have no experience whatsoever with coding, but I'm trying to adapt this scrypt so it can be used in version 1.1.0:

Code: [Select]
`#rotates model bounding box in accordance of coordinate system for active chunk#bounding box size is kept #compatibility: Agisoft PhotoScan Professional 0.9.0import PhotoScan import mathdoc = PhotoScan.app.documentchunk = doc.activeChunkT = chunk.transformv = PhotoScan.Vector( [0,0,0,1] )v_t = T * vv_t.size = 3m = chunk.crs.localframe(v_t)m = m * Ts = math.sqrt(m[0,0]*m[0,0] + m[0,1]*m[0,1] + m[0,2]*m[0,2]) #scale factor# S = PhotoScan.Matrix( [[s, 0, 0], [0, s, 0], [0, 0, s]] ) #scale matrixR = PhotoScan.Matrix( [[m[0,0],m[0,1],m[0,2]], [m[1,0],m[1,1],m[1,2]], [m[2,0],m[2,1],m[2,2]]])R = R * (1. / s)reg = chunk.regionreg.rot = R.t()chunk.region = reg`
So far I've made it into this:
Code: [Select]
`import PhotoScan import mathdoc = PhotoScan.app.documentchunk = doc.chunkT = chunk.transform.matrixv_t = T * PhotoScan.Vector( [0,0,0,1] )v_t.size = 3m = chunk.crs.localframe(v_t)m = m * Ts = math.sqrt(m[0,0]*m[0,0] + m[0,1]*m[0,1] + m[0,2]*m[0,2]) #scale factor# S = PhotoScan.Matrix( [[s, 0, 0], [0, s, 0], [0, 0, s]] ) #scale matrixR = PhotoScan.Matrix( [[m[0,0],m[0,1],m[0,2]], [m[1,0],m[1,1],m[1,2]], [m[2,0],m[2,1],m[2,2]]])R = R * (1. / s)reg = chunk.regionreg.rot = R.t()chunk.region = reg`
However I get the following error message:

line 16, in <module>
m = chunk.crs.localframe(v_t)
AttributeError: 'NoneType' object has no attribute 'localframe'

Any tips on how to overcome this issue?

Thomas

#### Alexey Pasumansky

• Agisoft Technical Support
• Hero Member
• Posts: 14012
##### Re: Align coordinate system to bounding box
« Reply #1 on: January 24, 2015, 03:56:01 PM »
Hello Thomas,

If the chunk is not referenced in geographic coordinate system (chunk.crs == None) you can just assign m = T.
Best regards,
Alexey Pasumansky,
Agisoft LLC

#### ThomasVD

• Full Member
• Posts: 107
##### Re: Align coordinate system to bounding box
« Reply #2 on: January 24, 2015, 06:55:09 PM »
Hey Alex,

Thanks for the quick reply, however it still doesn't appear to work. The script now looks like this:

Code: [Select]
`import PhotoScan import mathdoc = PhotoScan.app.documentchunk = doc.chunkT = chunk.transform.matrixv_t = T * PhotoScan.Vector( [0,0,0,1] )v_t.size = 3m = Ts = math.sqrt(m[0,0]*m[0,0] + m[0,1]*m[0,1] + m[0,2]*m[0,2]) #scale factor# S = PhotoScan.Matrix( [[s, 0, 0], [0, s, 0], [0, 0, s]] ) #scale matrixR = PhotoScan.Matrix( [[m[0,0],m[0,1],m[0,2]], [m[1,0],m[1,1],m[1,2]], [m[2,0],m[2,1],m[2,2]]])R = R * (1. / s)reg = chunk.regionreg.rot = R.t()chunk.region = reg`
But rather than changing the coordinate system it just seems to change the position of the bounding box (see same scene before and after running script, in attachment). When I go to predefined views -> top view, the top view is still exactly the same as it was before running the script

#### Alexey Pasumansky

• Agisoft Technical Support
• Hero Member
• Posts: 14012
##### Re: Align coordinate system to bounding box
« Reply #3 on: January 24, 2015, 07:20:30 PM »
Hello Thomas,

Then you just used the wrong script. Try this one (for all chunks in the workspace):

Code: [Select]
`#rotates model coordinate system in accordance of bounding box for all chunks in project#scale is kept #compatibility: Agisoft PhotoScan Professional 1.1.0import PhotoScan import math doc = PhotoScan.app.documentfor chunk in doc.chunks: R = chunk.region.rot #Bounding box rotation matrix C = chunk.region.center #Bounding box center vector if chunk.transform: T = chunk.transform.matrix s = math.sqrt(T[0,0]*T[0,0] + T[0,1]*T[0,1] + T[0,2]*T[0,2]) #scaling S = PhotoScan.Matrix( [[s, 0, 0, 0], [0, s, 0, 0], [0, 0, s, 0], [0, 0, 0, 1]] ) #scale matrix else: S = PhotoScan.Matrix( [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]] ) T = PhotoScan.Matrix( [[R[0,0],R[0,1],R[0,2], C[0]], [R[1,0],R[1,1],R[1,2],C[1]], [R[2,0],R[2,1],R[2,2],C[2]], [0,0,0,1]]) chunk.transform.matrix = S * T.inv() #resulting chunk transformation matrix`
Best regards,
Alexey Pasumansky,
Agisoft LLC

#### ThomasVD

• Full Member
• Posts: 107
##### Re: Align coordinate system to bounding box
« Reply #4 on: January 24, 2015, 07:44:43 PM »
Thank you Alexey, there is no way I would ever have been able to script that myself. Works perfectly.

If I'm ever in St Petersburg I want to buy you and the rest of the Agisoft team a few bottles of champagne, the service really is exceptional.

Thanks again

#### ktfank

• Newbie
• Posts: 6
##### Re: Align coordinate system to bounding box
« Reply #5 on: August 17, 2017, 03:55:37 AM »
I'm running into an issue with Alexey's script. It works, but then my camera errors are huge. When I update and/or calibrate the cameras, it flips the model upside down again. This is only happening when I am georeferencing with geotagged images. When I use GCPs instead, the model remains orientated correctly after I update the cameras.

#### Alexey Pasumansky

• Agisoft Technical Support
• Hero Member
• Posts: 14012
##### Re: Align coordinate system to bounding box
« Reply #6 on: August 17, 2017, 10:13:30 AM »
Hello ktfank,

The script is changing the orientation of the model in space, but when you use coordinates of the camera positions or markers to gereference it again, the previous changes applied by the script will be discarded.

You cannot have model referenced by two different approaches at the same time, so it may be reasonable to uncheck all cameras and markers in the Reference pane before running the script.
Best regards,
Alexey Pasumansky,
Agisoft LLC

#### ktfank

• Newbie
• Posts: 6
##### Re: Align coordinate system to bounding box
« Reply #7 on: December 15, 2017, 03:50:26 AM »
Another question for you Alexey: I am using a user-defined coordinate system for my models so that I can reference my GCPs/markers in NAVD88 (I downloaded geoid12a from the Agisoft  website). However now when I try to use this script to orient the bounding box to my markers I get an Error: "Vertical datum out of range". (I actually get the same error message printed repetitively, about 5 times.). This appears to be a problem only with the last update (1.3.4.5067) as I was successfully using the script with this same coordinate system before.

Is there a way to use the script with my chosen datums now?

Here are the parameters of my user-defined coordinate system:

Projection Method: Transverse Mercator
Latitude of natural origin: 0
Longitude of natural origin: -123
Scale factor at natural origin: 0.9996
False Easting: 500000
False Northing: 0