{"id":1728,"date":"2013-10-25T14:15:28","date_gmt":"2013-10-25T12:15:28","guid":{"rendered":"http:\/\/sickel.net\/blogg\/?p=1728"},"modified":"2014-01-03T12:45:10","modified_gmt":"2014-01-03T11:45:10","slug":"create-geojson-in-php","status":"publish","type":"post","link":"http:\/\/sickel.net\/blogg\/?p=1728","title":{"rendered":"Create GeoJSON in php"},"content":{"rendered":"<p><a href=\"http:\/\/geojson.org\/\">GeoJSON<\/a> is a text format for encoding various geographical structures. I have a large number of point data with  various extra information stored in a postgresql data base. I fetch all the data into an array in php, using PDO::FETCH_ASSOC, that is each row that has been fetched is an item in a numerical array, for each row,  the various fields are coded as an associative array, so I have a structure like<\/p>\n<pre>\r\nArray\r\n(\r\n    [0] => Array\r\n        (\r\n            [id] => 210\r\n            [name] => Sellafield\r\n            [lat] => 54.43\r\n            [lon] => -3.52\r\n            [type] => Reprocessing plant\r\n            [country] => Great Britain\r\n        )\r\n\r\n    [1] => Array\r\n        (\r\n            [id] => 177\r\n            [name] => Dounreay\r\n            [lat] => 58.57\r\n            [lon] => -3.73\r\n            [type] => Reprocessing plant\r\n            [country] => Great Britain\r\n        )\r\n)\r\n<\/pre>\n<p>Now I want to use the lat and lon field to create the coordinate fields in GeoJSON and store the rest of the information as properties. Having the dataset stored as $dataset, I run the following code<\/p>\n<pre>\r\n$minlat=100;\r\n$maxlat=-100;\r\n$minlon=400;\r\n$maxlon=-200;\r\nforeach($dataset as &$item){ # Uses a pointer to refer to $item, so that changes are reflected in $dataset\r\n   $item['geometry']['type']='Point';\r\n   # Must make sure the coordinates are stored as numbers, not strings      \r\n   $lon=$item['lon']*1;\r\n   $lat=$item['lat']*1;\r\n   $item['geometry']['coordinates']=array($lon,$lat);\r\n   $minlon=min($minlon,$lon);                                                                                    \r\n   $maxlon=max($maxlon,$lon);                                                                                    \r\n   $minlat=min($minlat,$lat);                                                                                    \r\n   $maxlat=max($maxlat,$lat);                                                                                    \r\n   unset($item['lat']);\r\n   unset($item['lon']);\r\n   foreach($item as $k=>$v){  \r\n      # Don't want to mess with geometry as it is already set\r\n      # Id should also be set directly.\r\n      if(!($k='geometry' || $k=='id')) {\r\n         # Must make sure that numeric values are correctly handled\r\n         if(is_numeric($v)?$v*1:$v;\r\n         $props[$k]=$v;\r\n         unset($item[$k]);\r\n      }\r\n    }                                                                                                      \r\n    $item['type']='Feature';                                                                          \r\n    $item['properties']=$props;\r\n  }\r\n  $dataset=array(\"type\"=>\"FeatureCollection\",\"features\"=>$dataset);\r\n  $dataset['bbox']=array($minlon,$minlat,$maxlon,$maxlat);\r\n  # Defining crs for WGS84 lat\/lon\r\n  $dataset['crs']=array(\"type\"=>\"name\",\"properties\"=>array(\"name\",\"urn:ogc:def:crs:EPSG::4326\"));  \r\n$json=json_encode($dataset);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>GeoJSON is a text format for encoding various geographical structures. I have a large number of point data with various extra information stored in a postgresql data base. I fetch all the data into an array in php, using PDO::FETCH_ASSOC, &hellip; <a href=\"http:\/\/sickel.net\/blogg\/?p=1728\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-1728","post","type-post","status-publish","format-standard","hentry","category-div"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnVtD-rS","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1728"}],"version-history":[{"count":10,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1728\/revisions"}],"predecessor-version":[{"id":1791,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=\/wp\/v2\/posts\/1728\/revisions\/1791"}],"wp:attachment":[{"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1728"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sickel.net\/blogg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}