Converting coordinates with cs2cs

In the spirit of Blog Little Things and after reading WEBKID’s You Might Not Need QGIS earlier, here you go:

So you have thousands or millions of coordinates in “the wrong” or “that stupid” coordinate reference system and want to convert them to “the one you need”? Easily done with proj‘s cs2cs tool. You feed it a list of coordinates and tell it how you want them transformed and put out.

Let’s say you have these crazy accurate super coordinates in EPSG:4326 (WGS84) in a file called MyStupidCoordinates.txt (for example from copying columns out of Excel).


9.92510775592794303 53.63833616755876932
9.89715616602172332 53.61639299685715798
9.91541274879985579 53.63589289755918799
9.91922922611725966 53.63415202989637010
9.92072211107566382 53.63179675033637750
9.89998015840083490 53.62284810375095390
9.90427723676020832 53.60740624866674153
9.95012889485460583 53.64563499608360075
9.89590860878436196 53.62979225409544171
9.92944379841924452 53.60061248161031955

and you want them in the much superior and wonderfully metric EPSG:25832 (ETRS89 / UTM zone 32N). You simply use +init=sourceCRS +to +init=targetCRS. if you have no idea what CRS your coordinates are in, enjoy 5 minutes with http://projfinder.com/ and you will either know or you might want to take a walk (don’t forget to try them flipped though).

cs2cs +init=epsg:4326 +to +init=epsg:25832 MyStupidCoordinates.txt

and it will print


561164.00 5943681.64 0.00
559346.79 5941216.81 0.00
560526.52 5943401.54 0.00
560781.36 5943211.12 0.00
560883.46 5942950.37 0.00
559524.51 5941937.29 0.00
559830.54 5940223.00 0.00
562807.40 5944515.43 0.00
559245.50 5942706.43 0.00
561505.49 5939488.65 0.00

You probably want more decimal places though, since your input coordinates were accurate down to the attodegree. For this, you can specify the output format with -f

cs2cs +init=epsg:4326 +to +init=epsg:25832 -f "%.17f" MyStupidCoordinates.txt


561164.00100000295788050 5943681.64279999211430550 0.00000000000000000
559346.79200000269338489 5941216.80899999570101500 0.00000000000000000
560526.52400000276975334 5943401.53899999428540468 0.00000000000000000
560781.36300000245682895 5943211.12199999392032623 0.00000000000000000
560883.46400000224821270 5942950.37499999348074198 0.00000000000000000
559524.51200000231619924 5941937.29399999510496855 0.00000000000000000
559830.54200000269338489 5940223.00299999490380287 0.00000000000000000
562807.39800000295508653 5944515.43399999290704727 0.00000000000000000
559245.50000000221189111 5942706.42899999395012856 0.00000000000000000
561505.48800000257324427 5939488.65499999187886715 0.00000000000000000

Perfect!

You could direct these transformed coordinates into a new file called MyCoolCoordinates.txt by adding a redirection of its output:

cs2cs +init=epsg:4326 +to +init=epsg:25832 MyStupidCoordinates.txt > MyCoolCoordinates.txt

You can find out more about cs2cs by reading its manpage:

man cs2cs

PS: You can handle that Z coordinate, right?

7 thoughts on “Converting coordinates with cs2cs

    1. Hannes

      Hi reza, unfortunately I don’t understand your question. Could you ask it again differently or maybe with more detail? :)

      Reply
  1. Dorian Oria

    Hello. I got the following error:
    Using from definition: init=epsg:4326
    Rel. 5.2.0, September 15th, 2018
    :
    projection initialization failure
    cause: invalid projection system error (-9999)
    program abnormally terminated

    Reply
    1. Hannes Post author

      Sounds like your cs2cs is not finding its database. Try reinstalling it or use a different installation method.

      Reply
  2. Perry J Hardin

    Reza,

    I agree that cs2cs is indeed not finding its epsg database. If you don’t want to change the PROJ_LIB setting (see docs), you can use the full path to the epsg data on disk in the command line. This I demonstrate.

    In the example, note the use of quotes in specifying complete path names. This was necessary in the case of the cs2cs program path because of embedded spaces in the path. You will always be safe is you use quotes whenever you specify a full path, even if the quotes are not strictly necessary. In my example below, I did NOT need the quotes in specifying the +init options, because the paths had no empty spaces, but I used them nonetheless for demonstration purposes.

    The example is obviously windows, and the epsg libraries and cs2cs are either in my GRASS or QGIS folders. Yours may be elsewhere. Find them first so you know those complete path names before you compose the command.

    The example will work regardless of what current working directory you execute it from. That is the beauty of using full path names. If you use this kind of construct a lot, you might put it into a .cmd file so you can edit and re-run it with minimum hassle.

    The example converts a lat / lon in EPSG 4326 to a UTM coordinate in EPSG 32737. The answers should be 358414.50mE 9585403.34mN, southern hemisphere, UTM grid zone 37.

    echo -3.750000 37.725000 | “C:\Program Files\GRASS GIS 7.6.0\extrabin\cs2cs.exe” +init=”C:\QGIS34\share\proj\epsg”:4326 +to +init=”C:\QGIS34\share\proj\epsg”:32737 -r

    Reply
  3. Brian

    I would like to convert lat/lon coordinates from old (pre-1970) UK Admiralty charts to lat/lon in WGS84. For GB coastal waters I guess the datum will be OSGB 1936, which looks like EPSG:4277. Can you help with the cs2cs syntax for that? Also do you know what the datum would be for worlwide charts?

    Thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.