Through the Python API, you can define a coordinate system for the project. The article describes general cases:


Metashape Python API is available on our website - https://www.agisoft.com/downloads/user-manuals/


How to set a coordinate system for chunk?


1. You can use Metashape.app.getCoordinateSystem() call to open coordinate system selection dialog to user:


chunk.marker_crs = Metashape.CoordinateSystem("EPSG::4326")


2. Use EPSG registry. Metashape Professional supports most of coordinate systems from EPSG registry (accessible by EPSG code in Coordinate System selection dialog) and Cartesian coordinate systems (local coordinate systems):


import Metashape
chunk = Metashape.app.document.chunk
chunk.crs=Metashape.CoordinateSystem("EPSG::4326")



3. Use WKT. Additionally it is possible to import the coordinate system information defined in Well-Known Text (WKT) format 

from the PRJ file. 


import Metashape
chunk = Metashape.app.document.chunk
path_prj = "C:/Users/coordinatesystem.prj"
file = open(path_prj, "rt")
wkt = file.readlines()
wkt = wkt[0]
chunk.crs = Metashape.CoordinateSystem(wkt)


or


import Metashape
chunk = Metashape.app.document.chunk
chunk.crs=Metashape.CoordinateSystem('GEOGCS["WGS 84",DATUM["World Geodetic System 1984 ensemble",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9102"]],AUTHORITY["EPSG","4326"]]')


How to set a coordinate system for cameras and markers?


Also via Python it is possible to set coordinate system for cameras in the project and markers. 


If the coordinates of the markers and images are obtained in the same coordinate system that you specified for the chunk, then you do not need to additionally specify the coordinate system for the images and markers in the script. In this case, the same coordinate system as for the chunk will be used automatically.



For cameras:

chunk.camera_crs = Metashape.CoordinateSystem("EPSG::4326")


For markers:

chunk.marker_crs = Metashape.CoordinateSystem("EPSG::4326")


This will correspond to the Reference Settings dialog window when the coordinate systems for the chunk, markers, and cameras are defined:


It is important to add the geoid before performing any operations related to executing commands associated with the coordinate system. We recommend importing the geoid at the very beginning of the script.



How to add a geoid and use the coordinate system with the geoid?


The addgeoid() command is used to initialize the path to the geoid file. To set the coordinate system, we recommend using the prj file:

import Metashape
chunk = Metashape.app.document.chunk 
path_geoid = "geoid.tif" #or you can use Metashape.app.getOpenFileName() command
path_prj = "crs.prj" #or you can use Metashape.app.getOpenFileName()
file = open(path_prj, "rt")
wkt = file.readlines()
wkt = wkt[0]
Metashape.CoordinateSystem().addGeoid(path_geoid)
chunk.crs = Metashape.CoordinateSystem(wkt)



You can download the geoid file from our website for certain coordinate systems - https://www.agisoft.com/downloads/geoids/



How to convert coordinates?


To transform coordinates  you need to use  Metashape.CoordinateSystem().transform(coord, original_crs, target_crs) command

For example: 
import Metashape
chunk = Metashape.app.document.chunk
original_crs = Metashape.CoordinateSystem("EPSG::1") #coordinatesystem 1
original_crs.towgs84 =  [1,2,3,4,5,6,7]  #towgs84 parameters for 1 coordinate system
target_crs = Metashape.CoordinateSystem("EPSG::2") #coordinatesystem 2
target_crs.towgs84 =  [0,0,0,0,0,0,0] #towgs84 parameters for 2 coordinate system
for camera in chunk.cameras:
    coord = camera.reference.location
    if not coord:
        continue #skip, if no reference coordinates
    camera.reference.location = Metashape.CoordinateSystem().transform(coord, original_crs, target_crs)
chunk.crs = target_crs
chunk.updateTransform()



How to define a local coordinate system?


Local Coordinates (m)

import Metashape
chunk = Metashape.app.document.chunk
chunk.crs=Metashape.CoordinateSystem('LOCAL_CS["Local Coordinates (m)",LOCAL_DATUM["Local Datum",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]]]')


Local Coordinates (ftUS)

import Metashape
chunk = Metashape.app.document.chunk
chunk.crs=Metashape.CoordinateSystem('LOCAL_CS["Local Coordinates (ftUS)",LOCAL_DATUM["Local Datum",0],UNIT["US survey foot",0.3048006096012192,AUTHORITY["EPSG","9003"]]]')


Local Coordinates (ft)

import Metashape
chunk = Metashape.app.document.chunk
chunk.crs=Metashape.CoordinateSystem('LOCAL_CS["Local Coordinates (ft)",LOCAL_DATUM["Local Datum",0],UNIT["foot",0.3048,AUTHORITY["EPSG","9002"]]]')