{"id":71,"date":"2007-07-14T17:40:55","date_gmt":"2007-07-14T22:40:55","guid":{"rendered":"http:\/\/www.kralidis.ca\/blog\/2007\/07\/14\/reprojecting-in-python\/"},"modified":"2007-07-14T18:41:57","modified_gmt":"2007-07-14T23:41:57","slug":"reprojecting-in-python","status":"publish","type":"post","link":"https:\/\/www.kralidis.ca\/blog\/2007\/07\/14\/reprojecting-in-python\/","title":{"rendered":"Reprojecting in Python"},"content":{"rendered":"<p>My <a href=\"\/blog\/2007\/06\/18\/getting-into-python\/\" title=\"previous post\">previous post<\/a> declared my plugging my nose and jumping into Python GIS stuff.   <a href=\"http:\/\/zcologia.com\/news\/488\/diving-into-python\/\" title=\"Advice\">Advice <\/a>and <a href=\"http:\/\/www.gispython.org\/\" title=\"info\">info<\/a> from <a href=\"http:\/\/zcologia.com\/news\/\" title=\"Sean\">Sean<\/a> have been valuable in getting familiar with things, though I&#8217;ll be the first to admit I&#8217;m still green (writing Python like a Perl \/ C hack \ud83d\ude42 ).<\/p>\n<p>So here&#8217;s one of my first attempts at solving a real world problem: reprojecting a bunch of points from a CSV file:<\/p>\n<pre>\r\n#!\/usr\/bin\/python<\/pre>\n<pre>import sys\r\nimport mapscript\r\n\r\nif (len(sys.argv) == 1):<\/pre>\n<pre>\tprint sys.argv[0] + \" &lt;csvfile&gt;\"\r\n\tsys.exit(1)\r\nelse:\r\n\tprojInObj  = mapscript.projectionObj(\"init=epsg:32619\")\r\n\tprojOutObj = mapscript.projectionObj(\"init=epsg:4326\")<\/pre>\n<pre>\tprint \"id,geom,zone\"\r\n\tf = open(sys.argv[1], 'r')\r\n\tfor line in f:\r\n\t\ts = line.strip()\r\n\t\tk = s.split(\",\")\r\n\t\twkt = \"POINT(\" + k[1] + \" \" + k[2] + \")\"\r\n\t\tshape = mapscript.shapeObj.fromWKT(wkt) # man, I love WKT!!\r\n\t\tshape.project(projInObj, projOutObj)\r\n\t\tprint k[0] + \",\" + shape.toWKT() + \",\" + k[3]\r\n\tf.close()<\/pre>\n<p>And that&#8217;s it!  So now you can take the CSV output with the geometry encoded as WKT and hook it up to <a href=\"http:\/\/mapserver.gis.umn.edu\/\" title=\"MapServer\">MapServer<\/a> with some simple OGR OVF syntax in your mapfile:<\/p>\n<pre>\r\nCONNECTIONTYPE OGR\r\nCONNECTION \"&lt;OGRVRTDataSource&gt;\r\n&lt;OGRVRTLayer name='nb'&gt;\r\n&lt;SrcDataSource&gt;.\/nb.csv&lt;\/SrcDataSource&gt;\r\n&lt;GeometryType&gt;wkbPoint&lt;\/GeometryType&gt;\r\n&lt;GeometryField encoding='WKT' field='geom'\/&gt;\r\n&lt;\/OGRVRTLayer&gt;\r\n&lt;\/OGRVRTDataSource&gt;\"<\/pre>\n<p>Mind you, you might question why to reproject when MapServer can do this for you, but I digress.  There&#8217;s probably more eloquent ways to do this than what&#8217;s been done above via <a href=\"http:\/\/mapserver.gis.umn.edu\/docs\/reference\/mapscript\" title=\"mapscript\">mapscript<\/a>, eh?<\/p>\n<link rel=\"stylesheet\" href=\"http:\/\/cdn.leafletjs.com\/leaflet-0.5\/leaflet.css\" \/>\n<!--[if lte IE 8]>\n  <link rel=\"stylesheet\" href=\"http:\/\/cdn.leafletjs.com\/leaflet-0.5\/leaflet.ie.css\" \/>\n<![endif]-->\n<script src=\"http:\/\/cdn.leafletjs.com\/leaflet-0.5\/leaflet.js\"><\/script>\n<style type=\"text\/css\">#map71 { width: 300px; height: 200px; }<\/style>\n\n<div id=\"map71\"><\/div>\n<script type=\"text\/javascript\">\n  var map71 = L.map('map71').setView([43.620495, -79.513198], 10);\n  L.tileLayer('http:\/\/{s}.tile.osm.org\/{z}\/{x}\/{y}.png', {\n      attribution: '&copy; <a href=\"http:\/\/osm.org\/copyright\">OpenStreetMap<\/a> contributors'\n  }).addTo(map71);\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>My previous post declared my plugging my nose and jumping into Python GIS stuff. Advice and info from Sean have been valuable in getting familiar with things, though I&#8217;ll be the first to admit I&#8217;m still green (writing Python like a Perl \/ C hack \ud83d\ude42 ). So here&#8217;s one of my first attempts at [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7,3,11],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-geospatial","category-open-source","category-technology","category-web"],"_links":{"self":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":0,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}