{"id":823,"date":"2017-01-22T12:57:54","date_gmt":"2017-01-22T11:57:54","guid":{"rendered":"http:\/\/hannes.enjoys.it\/blog\/?p=823"},"modified":"2019-12-08T14:48:09","modified_gmt":"2019-12-08T13:48:09","slug":"what-does-it-look-like-if-you-move-all-countries-onto-the-same-location","status":"publish","type":"post","link":"https:\/\/hannes.enjoys.it\/blog\/2017\/01\/what-does-it-look-like-if-you-move-all-countries-onto-the-same-location\/","title":{"rendered":"What does it look like if you move all countries onto the same location?"},"content":{"rendered":"\n<p><strong>Note from 2019-12-07: This is neither efficient nor up-to-date with modern PROJ. Better don&#8217;t copy and paste but just take non-projection\/-transformation parts if you need them&#8230;<\/strong><\/p>\n\n\n<p>Sunday pre-lunch Python fun: What does it look like if you move all countries onto the same location?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"992\" height=\"611\" src=\"http:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/10.png\" alt=\"\" class=\"aligncenter size-full wp-image-824\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/10.png 992w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/10-250x154.png 250w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/10-768x473.png 768w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/10-700x431.png 700w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/10-120x74.png 120w\" sizes=\"auto, (max-width: 992px) 100vw, 992px\" \/><\/p>\n<pre line=\"1\" lang=\"python\">import fiona\nfrom shapely.geometry import *\nfrom fiona.crs import from_string\nfrom fiona.transform import transform_geom\nfrom shapely.affinity import translate\n\ndef get_biggest_polygon(multipolygon):\n    assert isinstance(geometry, MultiPolygon)\n    \n    max_area = 0\n    biggest_polygon = None\n    for polygon in geometry:\n        if polygon.area &gt; max_area:\n            max_area = polygon.area\n            biggest_polygon = polygon\n    return biggest_polygon\n    \ndef project_locally(geometry, from_crs):\n    \"\"\"centered on the centroid of the geometry\"\"\"\n    # ugly because i map\/shape back and forth, maybe try shapely instead\n    lat = geometry.centroid.y\n    lon = geometry.centroid.x\n    \n    to_crs = from_string(\"+proj=aeqd  +R=6371000 +lat_0={lat} +lon_0={lon}\".format(lat=lat, lon=lon))\n    reprojected_geometry = transform_geom(\n        from_crs, \n        to_crs, \n        mapping(geometry)\n    )\n    \n    return shape(reprojected_geometry)\n\nwith fiona.open(\"ne_10m_admin_0_countries.shp\") as countries:\n    centered_polygons = []\n\n    for country in countries:\n        geometry = shape(country['geometry'])\n\n        # only use the biggest part of each country, otherwise everything sucks\n        if isinstance(geometry, MultiPolygon):\n            polygon = get_biggest_polygon(geometry)\n        else:\n            polygon = geometry\n\n        # project nicely\n        polygon = project_locally(polygon, countries.crs)\n\n        # centering on 0,0 is simply moving the geometry by MINUS its x\/y\n        dx = -polygon.centroid.x\n        dy = -polygon.centroid.y\n        translated_polygon = translate(polygon, dx, dy)\n\n        centered_polygons.append(translated_polygon)\n\nwith open(\"\/tmp\/outfile.wkt\", \"w\") as sink:\n    for polygon in centered_polygons:\n        sink.write(polygon.wkt+\"\\n\")\n<\/pre>\n<p>Use this in any way you like but please share your creations and code as well. :)<\/p>\n<p>Some rough explanation: For each country I check if it is a multipolygon and if so, use only its biggest &#8220;sub&#8221;-polygon in the next steps. I then project the WGS84 coordinates to an Azimuthal Equidistant projection centered on the centroid of the polygon. That new geometry gets shifted to sit on the origin of the system. I collect all those polygons and write them as plain WKT to a file. Styling was done in QGIS.<\/p>\n<p>And for the smart folk, the same but without local projection:<br>\n<img loading=\"lazy\" decoding=\"async\" width=\"1612\" height=\"205\" src=\"http:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/f.png\" alt=\"\" class=\"aligncenter size-full wp-image-827\" srcset=\"https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/f.png 1612w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/f-250x32.png 250w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/f-768x98.png 768w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/f-700x89.png 700w, https:\/\/hannes.enjoys.it\/blog\/wp-content\/uploads\/f-120x15.png 120w\" sizes=\"auto, (max-width: 1612px) 100vw, 1612px\" \/><\/p>","protected":false},"excerpt":{"rendered":"<p>Note from 2019-12-07: This is neither efficient nor up-to-date with modern PROJ. Better don&#8217;t copy and paste but just take non-projection\/-transformation parts if you need them&#8230; Sunday pre-lunch Python fun: What does it look like if you move all countries onto the same location? import fiona from shapely.geometry import * from fiona.crs import from_string from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,29,43,44],"tags":[],"class_list":["post-823","post","type-post","status-publish","format-standard","hentry","category-fiona","category-gis","category-python","category-shapely"],"_links":{"self":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/comments?post=823"}],"version-history":[{"count":11,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/823\/revisions"}],"predecessor-version":[{"id":1375,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/posts\/823\/revisions\/1375"}],"wp:attachment":[{"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/media?parent=823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/categories?post=823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hannes.enjoys.it\/blog\/wp-json\/wp\/v2\/tags?post=823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}