Forum

Author Topic: DJI Reference Fixing  (Read 2711 times)

Hexx

  • Newbie
  • *
  • Posts: 4
    • View Profile
DJI Reference Fixing
« on: March 24, 2018, 11:42:31 AM »
I have had lots of problems getting Agisoft to correctly Align and reference photos taken from my DJI M210RTK and P3Pro.  I modified an existing script to fix the following issues:
Incorrect GPS Altitudes
Incorrect Rotational values (without the script exif data used is from the airframe and not the gimbal)
Compass errors, which flipped my LAT/LONG values
Code: [Select]
# Script to fix referencing for DJI Meta data for all cameras in the active chunk.
# this is an improved version of the script read_altitude_from_DJI_meta.py
# available at PhotoScan Pro Scripts repository: https://github.com/agisoft-llc/photoscan-scripts
# updated by H3xx1st (https://github.com/H3xx1st)

import PhotoScan

# Checking compatibility
compatible_major_version = "1.4"
found_major_version = ".".join(PhotoScan.app.version.split('.')[:2])
if found_major_version != compatible_major_version:
    raise Exception("Incompatible PhotoScan version: {} != {}".format(found_major_version, compatible_major_version))

def dji_refFix():
"""
Reads DJI/RelativeAltitude
fixes impossible Lat values due to inverted Lat/Long 
Sets the reference angles based upon the gimbal values
"""
doc = PhotoScan.app.document
if not len(doc.chunks):
raise Exception("No chunks!")
print("Script started...")
chunk = doc.chunk

for camera in chunk.cameras:
vect = camera.reference.rotation
rAccuracy = PhotoScan.Vector((10, 10, 10))
lAccuracy = PhotoScan.Vector((10, 10, 10))

# Sets the altitude to the RelativeAltitude
if 'DJI/RelativeAltitude' in camera.photo.meta.keys() and camera.reference.location:
z = float(camera.photo.meta["DJI/RelativeAltitude"])
lAccuracy[2] = float("0.5")

# Checks for an impossible latitude value and reverses the Lat/Long
if abs(camera.reference.location.y) > 90:
camera.reference.location = (camera.reference.location.y, camera.reference.location.x, z)
else:
camera.reference.location = (camera.reference.location.x, camera.reference.location.y, z)

# The camera/photo yaw, pitch, roll is taken from the exif data DJI/Gimbal
if 'DJI/GimbalYawDegree' in camera.photo.meta.keys():
vect[0] = float(camera.photo.meta["DJI/GimbalYawDegree"])
rAccuracy[0] = float("0.5")
if 'DJI/GimbalPitchDegree' in camera.photo.meta.keys():
vect[1] = float(camera.photo.meta["DJI/GimbalPitchDegree"]) + 90 # 90 Degrees is added to adjust for the flight angle
rAccuracy[1] = float("0.5")
if 'DJI/GimbalRollDegree' in camera.photo.meta.keys():
vect[2] = float(camera.photo.meta["DJI/GimbalRollDegree"])
rAccuracy[2] = float("0.5")

# Looks for an RTK Flag and sets accuracy accordingly
if 'DJI/RtkFlag' in camera.photo.meta.keys():
if camera.photo.meta("DJI/RtkFlag") == 1:
lAccuracy[0] = float("0.5")
lAccuracy[1] = float("0.5")
else:
lAccuracy[0] = float("2")
lAccuracy[1] = float("2")

camera.reference.rotation = vect
camera.reference.rotation_accuracy = rAccuracy
camera.reference.location_accuracy = lAccuracy

chunk.updateTransform()
PhotoScan.app.update()
print("Script finished")

label = "Custom menu/Fix DJI References"
PhotoScan.app.addMenuItem(label, dji_refFix)
print("To execute this script press {}".format(label))

Please let me know if this works for you, or if you have any improvements. My python skills are pretty basic so I wont be offended if anyone has even minor suggestions.

Known Bugs - My method of fixing the reversed lat/long only works if the reversal results in an invalid latitude

adam_s

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: DJI Reference Fixing
« Reply #1 on: March 28, 2018, 10:05:48 AM »
Thanks for your code.

I tried to test it, but...

1) "Reference fixing" just works for only first photo in the chunk (maybe i just did something wrong)

2) I'm not sure which Height Reference System did you use. Does your script just transform altitute from exif to real AGL (height above ground level)? Please explain

3) It is possible to use your code when only Long,Lat and Altitude elements are available? without angles?

Best regards,
Adam

Hexx

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: DJI Reference Fixing
« Reply #2 on: March 28, 2018, 12:14:39 PM »
Adam_S,
1) Maybe try selecting all of the photos in the chunk, and make sure they're enabled and checked in the reference window.
2) Read the code silly... Altitude used is the relative altitude. Which is referenced to the homepoint. Adjustments to aid in referencing to an existing co-ordinate system is not something I've had to do much of yet. So the relative altitude is good enough for me.
3) Probably, as the code looks if a tag is available and then changes the reference. No tag = no change.. I think. Save a backup try it and let me know if it works. If it messes things up you can always reload the exif data from the cameras.
 My coding skills are just good enough to get me by. So unfortunately I can't help that much. Good luck!