{"id":497,"date":"2010-06-25T14:14:23","date_gmt":"2010-06-25T18:14:23","guid":{"rendered":"http:\/\/www.kralidis.ca\/blog\/?p=497"},"modified":"2015-11-11T08:53:43","modified_gmt":"2015-11-11T13:53:43","slug":"geoprocessing-with-ogr-and-pywps","status":"publish","type":"post","link":"https:\/\/www.kralidis.ca\/blog\/2010\/06\/25\/geoprocessing-with-ogr-and-pywps\/","title":{"rendered":"Geoprocessing with OGR and PyWPS"},"content":{"rendered":"<p><a title=\"PyWPS\" href=\"http:\/\/pywps.wald.intevation.org\/\">PyWPS <\/a>is a neat Python package supporting the OGC <a title=\"Web Processing Service\" href=\"http:\/\/www.opengeospatial.org\/standards\/wps\">Web Processing Service<\/a> standard.\u00a0 Basic setup and configuration can be found in the documentation, or Tim&#8217;s useful <a title=\"post\" href=\"http:\/\/linfiniti.com\/2009\/09\/a-quick-quide-to-getting-up-and-running-with-pywps\/\">post<\/a>.<\/p>\n<p>I&#8217;ve been working on a demo to expose the OGR Python bindings for geoprocessing (buffer, centroid, etc.).<\/p>\n<p>Here&#8217;s an example process to buffer a geometry (input as WKT), and output either GML, JSON, or WKT:<\/p>\n<pre>from pywps.Process import WPSProcess\r\nimport osgeo.ogr as ogr\r\n\r\nclass Buffer(WPSProcess):\r\n def __init__(self):\r\n  WPSProcess.__init__(self,\r\n  identifier='buffer',\r\n  title='Buffer generator',\r\n  metadata=['http:\/\/www.kralidis.ca\/'],\r\n  profile='OGR \/ GEOS geoprocessing',\r\n  abstract='Buffer generator',\r\n  version='0.0.1',\r\n  storeSupported='true',\r\n  statusSupported='true')\r\n\r\n  self.wkt = self.addLiteralInput(identifier='wkt', \\\r\n   title='Well Known Text', type=type('string'))\r\n  self.format = self.addLiteralInput(identifier='format', \\\r\n   title='Output format', type=type('string'))\r\n  self.buffer = self.addLiteralInput(identifier='buffer', \\\r\n   title='Buffer Value', type=type(1))\r\n  self.out = self.addLiteralOutput(identifier='output', \\\r\n   title='Buffered Feature', type=type('string'))\r\n\r\n def execute(self):\r\n  buffer = ogr.CreateGeometryFromWkt( \\\r\n   self.wkt.getValue()).Buffer(self.buffer.getValue())\r\n  self.out.setValue(_genOutputFormat(buffer, self.format.getValue()))\r\n  buffer.Destroy()<\/pre>\n<pre> def _setNamespace(xml, prefix, uri):<\/pre>\n<pre>  return xml.replace('&gt;', ' xmlns:%s=\"%s\"&gt;' % (prefix, uri), 1)<\/pre>\n<pre> def _genOutputFormat(geom, format):<\/pre>\n<pre> if format == 'gml':<\/pre>\n<pre>  return _setNamespace(geom.ExportToGML(), 'gml', \\\r\n     'http:\/\/www.opengis.net\/gml')<\/pre>\n<pre> if format == 'json':<\/pre>\n<pre>  return geom.ExportToJson()<\/pre>\n<pre> if format == 'wkt':<\/pre>\n<pre>  return geom.ExportToWkt()<\/pre>\n<p>Notes:<\/p>\n<ul>\n<li>_setNamespace is a workaround, as OGR&#8217;s ExportToGML doesn&#8217;t declare a namespace prefix \/ uri in the output, which would make the ExecuteResponse XML choke parsers<\/li>\n<li>_genOutputFormat is a utility method, which can be applied to any OGR geometry object<\/li>\n<\/ul>\n<p>As you can see, very easy to pull off, integrates and extends easy.\u00a0 Kudos to the OGR and PyWPS teams!<\/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\">#map497 { width: 300px; height: 200px; }<\/style>\n\n<div id=\"map497\"><\/div>\n<script type=\"text\/javascript\">\n  var map497 = L.map('map497').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(map497);\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>PyWPS is a neat Python package supporting the OGC Web Processing Service standard.\u00a0 Basic setup and configuration can be found in the documentation, or Tim&#8217;s useful post. I&#8217;ve been working on a demo to expose the OGR Python bindings for geoprocessing (buffer, centroid, etc.). Here&#8217;s an example process to buffer a geometry (input as WKT), [&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-497","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\/497","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=497"}],"version-history":[{"count":8,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/497\/revisions"}],"predecessor-version":[{"id":659,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/497\/revisions\/659"}],"wp:attachment":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/media?parent=497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/categories?post=497"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/tags?post=497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}