a `ec@sddlZddlmZddlmZmZmZddlmZddZ ddZ d d Z d d Z d dZ ddZddZddZddZddZdS)N)log)Proj TransformerCRS)osrc Cst|dkrtdd}d}g}d}|D]}|jdus@|jdurRtd|jq(|durlt|j|j\}}z,|jdur~|jnd}t |j|j|||} Wntd|jYn0| | q(|durtdd} d} t t|} |D]$} | | d| 7} | | d| 7} qt t | } t t | } t|d h} | d ||f| d | | f|D].} | d | d| | d| | d fq^Wdn1s0YdS)aM Create a coordinate file containing the GPS positions of all cameras to be used later in the ODM toolchain for automatic georeferecing :param photos ([ODM_Photo]) list of photos :param images_path (str) path to dataset images :param output_coords_file (str) path to output coordinates file :return None rz@No input images, cannot create coordinates file of GPS positionsNz!GPS position not available for %sz,Failed to convert GPS position to UTM for %sz&No images seem to have GPS informationgwzWGS84 UTM %s%s z%s %s z %s %s %s )len Exceptionlatitude longituder ODM_WARNINGfilename get_utm_zone_and_hemisphere_fromaltitudeconvert_to_utmappendfloatintmathflooropenwrite)photos images_pathoutput_coords_fileutm_zone hemispherecoordsZreference_photophotoaltcoorddxdynumfr'/code/opendm/location.pyextract_utm_coordssB    r)cCst||||dddS)Nrr  transformerTransformPoint)from_srsto_srsxyr'r'r( transform2;sr1cCst|||||SNr*)r-r.r/r0zr'r'r( transform3>sr4cCsBt}|}|r ||n|}|||tj|S)z5 Convert a Proj SRS object to osr SRS object )rSpatialReferenceto_epsgImportFromEPSGto_proj4ImportFromProj4SetAxisMappingStrategyOAMS_TRADITIONAL_GIS_ORDER)srsresepsgproj4r'r'r(proj_srs_convertAs   r@cCst|}t|}t||Sr2)r@rCoordinateTransformation)r-r.srcZtgtr'r'r(r+Rsr+cCs6tt|dddd}|dkr*dnd}||gS)z Calculate the UTM zone and hemisphere that a longitude/latitude pair falls on :param lon longitude :param lat latitude :return [utm_zone, hemisphere] gf@g@<rrSN)rrrlonlatrrr'r'r(rWsrcCsD|dkrtd|ddd}ntd|dddd}|||\}}|||gS)z Convert longitude, latitude and elevation values to UTM :param lon longitude :param lat latitude :param alt altitude :param utm_zone UTM zone number :param hemisphere one of 'N' or 'S' :return [x,y,z] UTM coordinates rEZutmWGS84T)projzoneellpspreserve_units)rJrKrLrMsouth)r)rGrHr!rrpr/r0r'r'r(rbs rc Cs6|}|d}z|ddkr|ddkr|d}|dt|dd}t|ddt|dd}||d}d }|d kr|d 7}t|jfi|}nLd |vrt|d }n2| drt |dd}n t d|Wn<t y0}z"t d|t |WYd}~n d}~00|S)zp Parse a header coming from GCP or coordinate file :param header (str) line :return Proj object  rrIrZUTMr N)rKdatumz<+proj=utm +zone={zone} +datum={datum} +units=m +no_defs=TruerDz +south=Truez+proj'zepsg:z1Could not parse coordinates. Bad SRS supplied: %saUh oh! There seems to be a problem with your coordinates/GCP file. The line: %s Is not valid. Projections that are valid include: - EPSG:***** - WGS84 UTM **(N|S) - Any valid proj4 string (for example, +proj=utm +zone=32 +north +ellps=WGS84 +datum=WGS84 +units=m +no_defs) Some valid EPSG codes are not yet available in OpenDroneMap and need substituted with valid proj4 strings Try searching for equivalent proj4 strings at spatialreference.org or epsg.io. Modify your input and try again.)stripsplitr upperrr from_proj4formatlower startswith from_epsg RuntimeErrorr ODM_ERROR) headerrefrQZutm_polerZ proj_argsr?r<er'r'r(parse_srs_headerts2   racCst||\}}td||fS)NzWGS84 UTM %s%s)rrarFr'r'r(utm_srs_from_llsrbcCs0td}t||}t||}t||}||fS)Ni)rr[rbr+)rGrHZ source_srsZ target_srsZ ll_to_utmZ utm_to_llr'r'r(utm_transformers_from_lls     rc)ropendmrpyprojrrrosgeorr)r1r4r@r+rrrarbrcr'r'r'r(s  5 -