Last active
December 2, 2019 16:30
-
-
Save kauevestena/a7355496407df1cbc25258f4ce1368b2 to your computer and use it in GitHub Desktop.
intended to translate from one CRS to another in C language
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #include <stdio.h> | |
#include <C:\OSGeo4W64\include\ogr_srs_api.h> | |
/* | |
2019 Kaue de Moraes Vestena | |
Intended for integration in C code | |
*/ | |
struct lat_lgt | |
{ | |
// struct to store the result for the version 1 | |
double lat; | |
double lgt; | |
}; | |
struct lat_lgt transform_lat_lgt_v1(double E,double N,int dest_EPSG) | |
{ | |
/* | |
Transform a pair of projected E,N coordinates from a Coordinate Reference System to WGS84 Longitude,Latitude | |
arguments: | |
E : input X coordinate | |
Y : input Y coordinate | |
dest_EPSG : EPSG code for the source CRS | |
for more infos about EPSG codes, visit: http://epsg.io/ and the wikipedia article | |
*/ | |
// basic declarations | |
struct lat_lgt result; | |
double lat,lgt,h; | |
// GDAL declarations | |
OGRSpatialReferenceH DEST_CRS,WGS84_T; | |
DEST_CRS = OSRNewSpatialReference(NULL); | |
WGS84_T = OSRNewSpatialReference(NULL); | |
// creating the spatial references | |
OSRImportFromEPSG(DEST_CRS,dest_EPSG); | |
OSRImportFromEPSG(WGS84_T,4326); | |
// the transformer object | |
OGRCoordinateTransformationH the_transf = OCTNewCoordinateTransformation(WGS84_T,DEST_CRS); | |
// doing the transformation | |
OCTTransform(the_transf, 1, &lgt, &lat, &h); | |
result.lat = lat; | |
result.lgt = lgt; | |
return result; | |
/* | |
SAMPLE RUN: | |
double Es = 670862.352; | |
double Ns = 7185074.028; | |
int UTM22S_SIRGAS = 31982; | |
lat_lgt transformed = transform_lat_lgt_v1(Es,Ns,UTM22S_SIRGAS) | |
************* | |
transformed.lat : -25.44151 | |
transformed.lgt : -49.30082 | |
*/ | |
} | |
char *transform_lat_lgt_v2(char *E,char *N,char *dest_EPSG) | |
{ | |
/* | |
Transform a pair of projected E,N coordinates from a Coordinate Reference System to WGS84 Longitude,Latitude | |
second version that calls GDAL CLI tool, so we don't need to import any libraries | |
arguments: | |
E : input X coordinate AS C-STR | |
Y : input Y coordinate AS C-STR | |
dest_EPSG : EPSG code for the source CRS AS C-STR | |
for more infos about EPSG codes, visit: http://epsg.io/ and the wikipedia article | |
*/ | |
// printf("%s %s %s",E,N,dest_EPSG); | |
// predeclarations | |
char buffer [500]; | |
char *outstr = malloc(100); | |
int cx; | |
// formatting the string containing the command | |
cx = snprintf(buffer,499,"echo %s %s | gdaltransform -s_srs EPSG:%s -t_srs EPSG:4326 -output_xy",E,N,dest_EPSG); | |
// popen command to store output of command line tool | |
FILE *fp; | |
fp = popen(buffer,"r"); | |
//passing from popen stream to string | |
while (fgets(outstr, 500, fp) != NULL) | |
{ | |
// nothing to do inside | |
} | |
return outstr; | |
/* | |
sample run: | |
char strE[] = "670862.352" ; | |
char strN[] = "7185074.028"; | |
char strEPSG[] = "31982"; | |
char *out; | |
out = transform_lat_lgt_v2(strE,strN,strEPSG); | |
printf("%s", out); | |
****************** | |
should print: | |
-49.3008152048423 -25.4415134529186 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment