{"id":287,"date":"2009-03-23T10:27:39","date_gmt":"2009-03-23T15:27:39","guid":{"rendered":"http:\/\/www.kralidis.ca\/blog\/?p=287"},"modified":"2009-03-24T07:03:48","modified_gmt":"2009-03-24T12:03:48","slug":"fun-with-shapelib","status":"publish","type":"post","link":"https:\/\/www.kralidis.ca\/blog\/2009\/03\/23\/fun-with-shapelib\/","title":{"rendered":"fun with Shapelib"},"content":{"rendered":"<p>We have some existing C modules which do a bunch of data processing, and wanted the ability to spit out shapefiles on demand.\u00a0 <a title=\"Shapelib\" href=\"http:\/\/shapelib.maptools.org\/\">Shapelib<\/a> is a C library which allows for reading and writing shapefiles and dbf files.\u00a0 Thanks to the <a title=\"API\" href=\"http:\/\/shapelib.maptools.org\/shp_api.html\">API<\/a> <a title=\"docs\" href=\"http:\/\/shapelib.maptools.org\/dbf_api.html\">docs<\/a>, here&#8217;s a pared down version of how to write a new point shapefile (with, in this case, one record):<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;libshp\/shapefil.h&gt;<\/pre>\n<pre>\/*\r\n build with: gcc -O -Wall -ansi -pedantic -g -L\/usr\/local\/lib -lshp foo.c\r\n*\/<\/pre>\n<pre>int main() {\r\n\u00a0\u00a0\u00a0 int i = 0;\r\n\u00a0\u00a0\u00a0 double *x;\r\n\u00a0\u00a0\u00a0 double *y;\r\n\r\n\u00a0\u00a0\u00a0 SHPHandle\u00a0 hSHP;\r\n\u00a0\u00a0\u00a0 SHPObject *oSHP;\r\n\u00a0\u00a0\u00a0 DBFHandle\u00a0 hDBF;\r\n\r\n\u00a0\u00a0\u00a0 x = malloc(sizeof(*x));\r\n\u00a0\u00a0\u00a0 y = malloc(sizeof(*y));\r\n\r\n\u00a0\u00a0\u00a0 \/* create shapefile and dbf *\/\r\n\u00a0\u00a0\u00a0 hSHP = SHPCreate(\"bar\", SHPT_POINT);\r\n\u00a0\u00a0\u00a0 hDBF = DBFCreate(\"bar\");\r\n\r\n\u00a0\u00a0\u00a0 DBFAddField(hDBF, \"stationid\", FTString, 25, 0);\r\n\r\n\u00a0\u00a0\u00a0 \/* add record *\/\r\n\u00a0\u00a0\u00a0 x[0] = -75;\r\n\u00a0\u00a0\u00a0 y[0] = 45;\r\n\u00a0\u00a0\u00a0 oSHP = SHPCreateSimpleObject(SHPT_POINT, 1, x, y, NULL);\r\n\u00a0\u00a0\u00a0 SHPWriteObject(hSHP, -1, oSHP);\r\n\u00a0\u00a0\u00a0 DBFWriteStringAttribute(hDBF, 0, 0, \"abcdef\");\r\n\r\n\u00a0\u00a0\u00a0 \/* destroy *\/\r\n\u00a0\u00a0\u00a0 SHPDestroyObject(oSHP);\r\n\r\n\u00a0\u00a0\u00a0 \/* close shapefile and dbf *\/\r\n\u00a0\u00a0\u00a0 SHPClose(hSHP);\r\n\u00a0\u00a0\u00a0 DBFClose(hDBF);\r\n\u00a0\u00a0\u00a0 free(x);\r\n\u00a0\u00a0\u00a0 free(y);\r\n\r\n\u00a0\u00a0\u00a0 return 0;\r\n}<\/pre>\n<p>Done!<\/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\">#map287 { width: 300px; height: 200px; }<\/style>\n\n<div id=\"map287\"><\/div>\n<script type=\"text\/javascript\">\n  var map287 = L.map('map287').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(map287);\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>We have some existing C modules which do a bunch of data processing, and wanted the ability to spit out shapefiles on demand.\u00a0 Shapelib is a C library which allows for reading and writing shapefiles and dbf files.\u00a0 Thanks to the API docs, here&#8217;s a pared down version of how to write a new point [&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],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-geospatial","category-open-source","category-technology"],"_links":{"self":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/287","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=287"}],"version-history":[{"count":13,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":294,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/posts\/287\/revisions\/294"}],"wp:attachment":[{"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kralidis.ca\/blog\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}