Difference between revisions of "Proj4"

From SCECpedia
Jump to navigationJump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
Coordinate system conversions are needed for several SCEC projects.  
 
Coordinate system conversions are needed for several SCEC projects.  
  
== Example Conversions ==
+
== Example Conversion from UTM to X/Y/Z Coordinates ==
 +
* Define origin point for X/Y/Z coordinates
 +
* translate UTM to X/Y/Z
 +
 
 +
== Example Conversions Lat/Lon to UTM ==
 
Converting WGS84 to UTM Zone 11 using cs2cs
 
Converting WGS84 to UTM Zone 11 using cs2cs
  
* cs2cs -v +proj=latlong +datum=WGS84 +ellps=WGS84 +to +proj=utm +zone=11 +datum=WGS84 +ellps=WGS84"
+
* cs2cs -v +proj=latlong +datum=WGS84 +to +proj=utm +zone=11 +datum=WGS84
 
<pre>
 
<pre>
 
-bash-4.2$ cs2cs -v +proj=latlong +datum=WGS84 +to +proj=utm +zone=11 +datum=WGS84
 
-bash-4.2$ cs2cs -v +proj=latlong +datum=WGS84 +to +proj=utm +zone=11 +datum=WGS84
Line 22: Line 26:
 
== Online Implementation ==
 
== Online Implementation ==
 
*[https://mygeodata.cloud/cs2cs/ CS2CS Online]
 
*[https://mygeodata.cloud/cs2cs/ CS2CS Online]
 +
 +
== Example Python Code ==
 +
 +
<pre>
 +
-bash-4.2$ more test_pyproj.py
 +
#!/usr/bin/env python
 +
#
 +
# Name: test_pyproj
 +
# Usage:
 +
#  Simple example showing how to use pyproj to convert from lat/lon to UTM zone 11
 +
#
 +
import pyproj
 +
 +
#define a projection with Proj4 Notation
 +
latlon_string="+proj=latlong +datum=WGS84 +ellps=WGS84"
 +
utm_string="+proj=utm +zone=11 +datum=WGS84 +ellps=WGS84"
 +
 +
latlon= pyproj.Proj(latlon_string)
 +
utm_s= pyproj.Proj(utm_string)
 +
 +
vals=[-118.0,34.0]
 +
x,y= pyproj.transform(latlon,utm_s,vals[0],vals[1])
 +
print "UTM E: ", x
 +
print "UTM N: ", y
 +
</pre>
 +
 +
== Example C Code and Makefile ==
 +
 +
<pre>
 +
#include <proj_api.h>
 +
 +
main(int argc, char **argv) {
 +
    projPJ pj_merc, pj_latlong;
 +
    double x, y;
 +
    int p;
 +
 +
    if (!(pj_merc = pj_init_plus("+proj=merc +ellps=clrk66 +lat_ts=33")) )
 +
        exit(1);
 +
    if (!(pj_latlong = pj_init_plus("+proj=latlong +ellps=clrk66")) )
 +
        exit(1);
 +
    while (scanf("%lf %lf", &x, &y) == 2) {
 +
        x *= DEG_TO_RAD;
 +
        y *= DEG_TO_RAD;
 +
        p = pj_transform(pj_latlong, pj_merc, 1, 1, &x, &y, NULL );
 +
        printf("%.2f\t%.2f\n", x, y);
 +
    }
 +
    exit(0);
 +
}
 +
</pre>
 +
 +
<pre>
 +
test_proj4.o: test_proj4.c
 +
gcc -o test_proj4 -lproj -L/home/scec-00/maechlin/workspace/proj4/lib -I/home/scec-00/maechlin/workspace/proj4/include test_
 +
proj4.c
 +
</pre>
  
  
Line 47: Line 106:
 
*[https://stackoverflow.com/questions/24950614/abnormal-output-when-using-proj4-to-transform-latlong-to-utm C example]
 
*[https://stackoverflow.com/questions/24950614/abnormal-output-when-using-proj4-to-transform-latlong-to-utm C example]
 
*[http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026 More C Examples]
 
*[http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026 More C Examples]
 +
*[http://tagis.dep.wv.gov/convert/ UTM Discussion]
  
 
== Downloads and Documentation ==
 
== Downloads and Documentation ==

Latest revision as of 18:30, 9 October 2017

Coordinate system conversions are needed for several SCEC projects.

Example Conversion from UTM to X/Y/Z Coordinates

  • Define origin point for X/Y/Z coordinates
  • translate UTM to X/Y/Z

Example Conversions Lat/Lon to UTM

Converting WGS84 to UTM Zone 11 using cs2cs

  • cs2cs -v +proj=latlong +datum=WGS84 +to +proj=utm +zone=11 +datum=WGS84
-bash-4.2$ cs2cs -v +proj=latlong +datum=WGS84 +to +proj=utm +zone=11 +datum=WGS84
# ---- From Coordinate System ----
#Lat/long (Geodetic alias)
#	
# +proj=latlong +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
# ---- To Coordinate System ----
#Universal Transverse Mercator (UTM)
#	Cyl, Sph
#	zone= south
# +proj=utm +zone=11 +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
-118.00 34.00
407650.40	3762606.66 0.00

Online Implementation

Example Python Code

-bash-4.2$ more test_pyproj.py
#!/usr/bin/env python
#
# Name: test_pyproj
# Usage:
#  Simple example showing how to use pyproj to convert from lat/lon to UTM zone 11
#
import pyproj

#define a projection with Proj4 Notation
latlon_string="+proj=latlong +datum=WGS84 +ellps=WGS84"
utm_string="+proj=utm +zone=11 +datum=WGS84 +ellps=WGS84"

latlon= pyproj.Proj(latlon_string)
utm_s= pyproj.Proj(utm_string)

vals=[-118.0,34.0]
x,y= pyproj.transform(latlon,utm_s,vals[0],vals[1])
print "UTM E: ", x
print "UTM N: ", y

Example C Code and Makefile

#include <proj_api.h>

main(int argc, char **argv) {
     projPJ pj_merc, pj_latlong;
     double x, y;
     int p;

     if (!(pj_merc = pj_init_plus("+proj=merc +ellps=clrk66 +lat_ts=33")) )
        exit(1);
     if (!(pj_latlong = pj_init_plus("+proj=latlong +ellps=clrk66")) )
        exit(1);
     while (scanf("%lf %lf", &x, &y) == 2) {
        x *= DEG_TO_RAD;
        y *= DEG_TO_RAD;
        p = pj_transform(pj_latlong, pj_merc, 1, 1, &x, &y, NULL );
        printf("%.2f\t%.2f\n", x, y);
     }
     exit(0);
}
test_proj4.o: test_proj4.c
	gcc -o test_proj4 -lproj -L/home/scec-00/maechlin/workspace/proj4/lib -I/home/scec-00/maechlin/workspace/proj4/include test_
proj4.c


FAQ

  • Online FAQ
  • what is the third return parameter in UTM values
    • input lat/long can include elevation. Third returned UTM param is elevation. When no lat/lon elevation used, utm return elev is 0.0
  • What is the +no_defs parameters?

Building Proj4

  • Proj4 built on Centos using the ./configure make, make install. Since we are building on a shared cluster, we needed to ./configure --prefix=/user/scec-00/maechlin/workspace/proj4 (installation path to local directory)
  • Installation of proj4 on Mac (high sierra) failed.
  • Installation of current proj4 on USC HPC succeeded.
  • Didn't try installing pyproj on Mac. It can use either installed proj4, or try to build its own
  • Used conda install pyproj on Anaconda 2 instance on USC HPC

Online Documentation

Downloads and Documentation

Related Entries