{"id":761,"date":"2012-11-13T13:22:52","date_gmt":"2012-11-13T13:22:52","guid":{"rendered":"http:\/\/all-geo.org\/volcan01010\/?p=761"},"modified":"2020-02-26T22:09:31","modified_gmt":"2020-02-26T22:09:31","slug":"change-coordinates-with-pyproj","status":"publish","type":"post","link":"https:\/\/all-geo.org\/volcan01010\/2012\/11\/change-coordinates-with-pyproj\/","title":{"rendered":"Easily change coordinate projection systems in Python with pyproj"},"content":{"rendered":"<p><a href=\"http:\/\/www.python.org\/\" target=\"_blank\" rel=\"noopener\">Python<\/a> is an easy-to-use programming language which, thanks to a growing number of cool extension modules, is really taking off in the world of scientific data handling.\u00a0 The <a href=\"https:\/\/trac.osgeo.org\/proj\/\">Proj4<\/a> libraries are a set of programs for performing coordinate system transformations.\u00a0 Both are open source, so you are free to install them on as many computers as you want and to share them with your friends.\u00a0 I had been using both for a while but only recently discovered the <a href=\"http:\/\/pyproj.googlecode.com\/svn\/trunk\/docs\/index.html\">pyproj<\/a> module that performs coordinate transformations inside Python itself.\u00a0 It makes life very easy.<\/p>\n<p>Pyproj can be installed via the pip package manager (<code>pip install pyproj<\/code>).<\/p>\n<h3>1.) Setting up coordinate systems<\/h3>\n<p>The first step is to define pyproj &#8216;objects&#8217; to represent the coordinate systems that you want to use.\u00a0 These can be defined using the Proj notation (see <a href=\"https:\/\/trac.osgeo.org\/proj\/wiki\/WikiStart#Documentation\" target=\"_blank\" rel=\"noopener\">Proj4 documentation<\/a> for details) but it is easier to set up commonly-used projections by referring to their standard code numbers.\u00a0 These are called EPSG codes and can be looked up on <a href=\"http:\/\/spatialreference.org\/\" target=\"_blank\" rel=\"noopener\">spatialreference.org<\/a>.<\/p>\n<pre class=\"brush: python; collapse: false; light: true; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nimport pyproj # Import the pyproj module \r\n \r\n# Define a projection with Proj4 notation, in this case an Icelandic grid \r\nisn2004=pyproj.CRS(&quot;+proj=lcc +lat_1=64.25 +lat_2=65.75 +lat_0=65 +lon_0=-19 +x_0=1700000 +y_0=300000 +no_defs +a=6378137 +rf=298.257222101 +to_meter=1&quot;) \r\n \r\n# Define some common projections using EPSG codes \r\nwgs84=pyproj.CRS(&quot;EPSG:4326&quot;) # LatLon with WGS84 datum used by GPS units and Google Earth \r\nosgb36=pyproj.CRS(&quot;EPSG:27700&quot;) # UK Ordnance Survey, 1936 datum \r\nUTM26N=pyproj.CRS(&quot;EPSG:32626&quot;) # UTM coords, zone 26N, WGS84 datum \r\nUTM27N=pyproj.CRS(&quot;EPSG:32627&quot;) # UTM coords, zone 27N, WGS84 datum \r\nUTM28N=pyproj.CRS(&quot;EPSG:32628&quot;) # ... you get the picture  \r\n<\/pre>\n<h3><strong>Note<\/strong>: older versions of pyproj use <code>pyproj.Proj(\"+init=EPSG:4326\")<\/code> syntax and <a href=\"https:\/\/pyproj4.github.io\/pyproj\/stable\/gotchas.html#axis-order-changes-in-proj-6\" rel=\"nofollow\">return coordinates in lon, lat order<\/a>.<\/h3>\n<h3>2.) Changing between different coordinate systems<\/h3>\n<p>In most cases, you will want to change between coordinate systems.\u00a0 This is even the case with GPS or GoogleEarth data, which use the specific WGS84 datum.\u00a0 Coordinate system changes are done with the <strong>transform<\/strong> function.<\/p>\n<pre class=\"brush: python; collapse: false; light: true; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npyproj.transform(wgs84, isn2004, 63.983, -19.700)                                                                                             \r\n# (1665725.2429655408, 186813.3884751596)\r\n<\/pre>\n<p>And when you have lots of data, transformations can be done with lists\/tuples\/arrays:<\/p>\n<pre class=\"brush: python; collapse: false; light: true; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nlon = &#x5B;-19.5, -19.7, -19.9] \r\nlat = &#x5B;63.183, 63.583, 63.983] \r\nxx, yy = pyproj.transform(wgs84, isn2004, lat, lon)                                                                                          \r\nxx                                                                                                                                           \r\n# &#x5B;1674812.314071126, 1665231.44553604, 1655933.043340466]\r\nyy                                                                                                                                           \r\n# &#x5B;97526.59264212535, 142220.3063699659, 186937.313488422]\r\n<\/pre>\n<p>It&#8217;s a simple as that.<\/p>\n<p>For other systems, check out the <a href=\"http:\/\/code.google.com\/p\/pyproj\/\" target=\"_blank\" rel=\"noopener\">pyproj website<\/a>.\u00a0 If you are playing with coordinate transforms, then it is likely that at some point you are going to want plot stuff on a map.\u00a0 Python can do maps, too;\u00a0 check out the Cartopy, Fiona and Shapely libraries.\u00a0 Taking things even further, see the following of how to do GIS analysis using only Python with the Geopandas library: <a href=\"https:\/\/github.com\/BritishGeologicalSurvey\/geopandas-demo\">https:\/\/github.com\/BritishGeologicalSurvey\/geopandas-demo<\/a><\/p>\n<h3>British National Grid and the OSGB36 datum.<\/h3>\n<p>People working in the UK may have to go through another step to convert their data into the British National Grid format (BNG), which uses two-letter codes to define <a href=\"http:\/\/sewhgpgc.co.uk\/os.php\" target=\"_blank\" rel=\"noopener\">100km-wide square regions<\/a> instead of presenting the full 13-figure coordinates.\u00a0 Thus <a href=\"http:\/\/en.wikipedia.org\/wiki\/Arthur%27s_Seat,_Edinburgh\" target=\"_blank\" rel=\"noopener\">Arthur&#8217;s Seat<\/a>, an extinct volcano in the centre of Edinburgh, has a BNG grid reference of NT2755072950.<\/p>\n<p>The original version of this blog post contained a code snippet that converted between alphanumeric grid references and OSGB36 coordinates.\u00a0 It has since been converted into a standalone Python module.\u00a0 For instructions on how to install and use it, see <a href=\"https:\/\/pypi.org\/project\/bng\/\">https:\/\/pypi.org\/project\/bng\/<\/a>.<\/p>\n<p>Happy mapping!<\/p>\n<p>Update 2019-02-26 to use pyproj version 2.5.0 syntax.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python is an easy-to-use programming language which, thanks to a growing number of cool extension modules, is really taking off in the world of scientific data handling.\u00a0 The Proj4 libraries are a set of programs for performing coordinate system transformations.\u00a0 &hellip; <a href=\"https:\/\/all-geo.org\/volcan01010\/2012\/11\/change-coordinates-with-pyproj\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-761","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/posts\/761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/comments?post=761"}],"version-history":[{"count":5,"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/posts\/761\/revisions"}],"predecessor-version":[{"id":1612,"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/posts\/761\/revisions\/1612"}],"wp:attachment":[{"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/media?parent=761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/categories?post=761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/all-geo.org\/volcan01010\/wp-json\/wp\/v2\/tags?post=761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}