{"id":228,"date":"2014-01-15T13:31:05","date_gmt":"2014-01-15T13:31:05","guid":{"rendered":"http:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=228"},"modified":"2014-04-19T14:46:24","modified_gmt":"2014-04-19T14:46:24","slug":"mapping-with-python-and-stata","status":"publish","type":"post","link":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=228","title":{"rendered":"Mapping with Python and Stata"},"content":{"rendered":"<p>Elevation data for large swathes of the planet have been collected by NASA and are available to download from <a href=\"http:\/\/http:\/\/dds.cr.usgs.gov\/srtm\/\">http:\/\/dds.cr.usgs.gov\/srtm\/<\/a>.<\/p>\n<p>The data is contained in binary files, each representing a 1-degree by 1-degree &#8220;square&#8221;. Here are five lines of Python and four lines of Stata that will turn the data into a simple graph:<\/p>\n<p><code>import struct<br \/>\nfile = open(\"data\/N52W011.hgt\", \"r\")<br \/>\nfor y in range(1201):<br \/>\nfor x in range(1201):<br \/>\nprint y, x, struct.unpack(\"&gt;h\",file.read(2))[0]<\/code><\/p>\n<p>Do <code>python file.py &gt; map.dat<\/code>. Then run this Stata code:<\/p>\n<p><code>infile i j height using \/tmp\/ext.dat<br \/>\ngen h2 = int(sqrt(height))<br \/>\nreplace h2 = 30 if h2&lt;=0<br \/>\nhmap j i h2, nosc<\/code><\/p>\n<p><img decoding=\"async\" alt=\"Low res version of map\" src=\"http:\/\/teaching.sociology.ul.ie\/bhalpin\/blogsrtm.png\" \/><\/p>\n<p>(Hi-res <a href=\"http:\/\/teaching.sociology.ul.ie\/bhalpin\/blogsrtmhi.pdf\">version<\/a>.)<\/p>\n<p>You may need to install Python&#8217;s <code>struct<\/code> package, and Stata&#8217;s <code>hmap<\/code> add on, but they&#8217;re available from the usual locations.<\/p>\n<p>There are better ways of doing this, of course: it&#8217;s slow, the aspect ratio is wrong, the colours are not ideal and the axis labelling is bad. Even worse, it is a complete abuse of the <code>hmap<\/code> add-on. It&#8217;s a quick and dirty way to turn binary data into pictures, all the same.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Elevation data for large swathes of the planet have been collected by NASA and are available to download from http:\/\/dds.cr.usgs.gov\/srtm\/. The data is contained in binary files, each representing a 1-degree by 1-degree &#8220;square&#8221;. Here are five lines of Python and four lines of Stata that will turn the data into a simple graph: import &hellip; <a href=\"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/?p=228\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Mapping with Python and Stata<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"footnotes":""},"categories":[6,4],"tags":[],"_links":{"self":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/228"}],"collection":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=228"}],"version-history":[{"count":14,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":249,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/249"}],"wp:attachment":[{"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teaching.sociology.ul.ie\/bhalpin\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}