{"id":73,"date":"2011-05-01T20:36:13","date_gmt":"2011-05-01T20:36:13","guid":{"rendered":"http:\/\/blogs.gentoo.org\/titanofold\/?p=73"},"modified":"2011-05-01T20:36:13","modified_gmt":"2011-05-01T20:36:13","slug":"postgis-users-rejoice","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/titanofold\/2011\/05\/01\/postgis-users-rejoice\/","title":{"rendered":"PostGIS Users Rejoice!"},"content":{"rendered":"<p>Over the past few days I have been working on a project for work when it suddenly dawned on me that PostGIS would do exactly what I want and be incredibly useful for some marketing-related information. So, I got to work on the PostGIS 1.5.2 ebuild.<br \/>\n<!--more--><br \/>\nSo, there&#8217;s some customer information that I need to clean up, among which I need to verify the zip codes. Or, at least fill in the city and state based on the zip code. (As I get more focused on database work, I get more frustrated with the loose restrictions on data entry that our software has.)<\/p>\n<p>Since this same data set I&#8217;m using is also used for other marketing reports, I figured it&#8217;d be kind of neat to determine how far away our customers are coming, or even to generate a report that reduces the number of potential mail outs to those who are within a certain radius of our location.<\/p>\n<p>So, I got cracking on the ebuild because now that I needed it. (Okay, wanted, but it&#8217;s a fine line.) The ebuild has been entirely revamped, and I think you PostGIS users may be quite happy with it.<\/p>\n<p>It properly works with slotted PostgreSQL with one caveat: You&#8217;ll need to reemerge PostGIS for each slot you want it to work with.<\/p>\n<pre style=\"font-size: 8pt\">root@host # postgresql-config set 9.0\r\nroot@host # emerge -av =dev-db\/postgis-1.5.2-r1\r\nroot@host # emacs \/etc\/postgis_dbs\r\nroot@host # emerge --config =dev-db\/postgis-1.5.2-r1\r\nroot@host # postgresql-config set 8.4\r\nroot@host # emerge -av =dev-db\/postgis-1.5.2-r1\r\nroot@host # emacs \/etc\/postgis_dbs\r\nroot@host # emerge --config =dev-db\/postgis-1.5.2-r1<\/pre>\n<p>And now that that&#8217;s done, I get to have some fun. First, I needed to find a zip code database that wouldn&#8217;t cost me huge bucks to get it from USPS themselves, or indeed any bucks at all. (Seriously, USPS can&#8217;t just give me a list of zip codes with the city name and state?) There are other options out there that don&#8217;t cost much, but I need that money to pay for food.<\/p>\n<p>Then I stumbled upon <a title=\"The Zip Code Database Project with Zip Code Distance Calculator\" href=\"http:\/\/zips.sourceforge.net\/\">The Zip Code Database Project<\/a> (zips.sourceforge.net). It has all the data I was looking for. (Side note: They need an alternative means to update the database as the U.S. Census Bureau won&#8217;t be providing that information anymore. So, if you can throw them a ) It has the zip codes, city names, states they&#8217;re in, and as a bonus the latitude and longitude for those which is precisely what I need to do distance calculations.<\/p>\n<p>I had to tweak things a bit in the SQL script they provide to get it to be proper for PostgreSQL and adjust the insert lines a bit to be proper for PostGIS. Resulting in a rather large file.<\/p>\n<p>Okay, okay, I&#8217;ll <a title=\"geo_zip_codes.sql\" href=\"http:\/\/dev.gentoo.org\/~titanofold\/geo_zip_codes.sql\">share it<\/a> with you.<\/p>\n<p>And once all that&#8217;s inserted, I can do some fun stuff.<\/p>\n<pre style=\"font-size: 8pt\">db=&gt; SELECT st_distance(gg1, gg2) * 0.0006214 AS miles\r\n  FROM (SELECT\r\n    (SELECT geoloc FROM geo_zip_codes WHERE zip = '35004') AS gg1,\r\n    (SELECT geoloc FROM geo_zip_codes WHERE zip = '28713') AS gg2\r\n  ) AS foo;\r\n      miles       \r\n------------------\r\n 212.304842735422\r\n(1 row)<\/pre>\n<p>That&#8217;s 212 miles as the crow flies, but good enough. Pretty neat I should say.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the past few days I have been working on a project for work when it suddenly dawned on me that PostGIS would do exactly what I want and be incredibly useful for some marketing-related information. So, I got to work on the PostGIS 1.5.2 ebuild.<\/p>\n","protected":false},"author":136,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[3,4],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1tO5a-1b","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/73"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/users\/136"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/comments?post=73"}],"version-history":[{"count":7,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/73\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/posts\/73\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/media?parent=73"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/categories?post=73"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/titanofold\/wp-json\/wp\/v2\/tags?post=73"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}