{"url":"https://api.github.com/gists/3682990","forks_url":"https://api.github.com/gists/3682990/forks","commits_url":"https://api.github.com/gists/3682990/commits","id":"3682990","node_id":"MDQ6R2lzdDM2ODI5OTA=","git_pull_url":"https://gist.github.com/3682990.git","git_push_url":"https://gist.github.com/3682990.git","html_url":"https://gist.github.com/mbostock/3682990","files":{"cleanup.js":{"filename":"cleanup.js","type":"text/javascript","language":"JavaScript","raw_url":"https://gist.githubusercontent.com/mbostock/3682990/raw/c90d9aabb55783c56009308741ffe94db5665865/cleanup.js","size":2409,"truncated":false,"content":"var fs = require(\"fs\");\n\nvar roundPrecision = 1e6;\n\nfs.readFile(\"/dev/stdin\", \"utf-8\", function(error, input) {\n  var collection = JSON.parse(input);\n  switch (collection.type) {\n    case \"FeatureCollection\": return cleanupFeatureCollection(collection);\n    case \"GeometryCollection\": return cleanupGeometryCollection(collection);\n    default: throw \"unknown type: \" + collection.type;\n  }\n});\n\nfunction cleanupFeatureCollection(collection) {\n  console.log(\"{\\\"type\\\":\\\"FeatureCollection\\\",\\\"features\\\":[\");\n  console.log(collection.features.map(cleanupFeature).map(stringify).join(\",\\n\"));\n  console.log(\"]}\");\n}\n\nfunction cleanupGeometryCollection(collection) {\n  console.log(\"{\\\"type\\\":\\\"GeometryCollection\\\",\\\"features\\\":[\");\n  console.log(collection.features.map(cleanupGeometry).map(stringify).join(\",\\n\"));\n  console.log(\"]}\");\n}\n\nfunction cleanupFeature(feature) {\n  if (feature.properties.ISO_A3) {\n    if (/[A-Z]{3}/.test(feature.properties.ISO_A3)) {\n      feature.id = feature.properties.ISO_A3;\n    } else {\n      console.warn(\"ignoring invalid ISO code for \" + feature.properties.NAME + \": \" + feature.properties.ISO_A3);\n    }\n  } else if (feature.properties.FIPS) {\n    if (/[0-9]{2}/.test(feature.properties.FIPS)) {\n      feature.id = feature.properties.FIPS;\n    } else {\n      console.warn(\"ignoring invalid FIPS code for \" + feature.properties.NAME + \": \" + feature.properties.FIPS);\n    }\n  }\n  feature.properties = {name: feature.properties.NAME};\n  cleanupGeometry(feature.geometry);\n  return feature;\n}\n\nfunction cleanupGeometry(geometry) {\n  switch (geometry.type) {\n    case \"Polygon\": geometry.coordinates = roundPolygon(geometry.coordinates); break;\n    case \"MultiPolygon\": geometry.coordinates = roundMultiPolygon(geometry.coordinates); break;\n    case \"LineString\": geometry.coordinates = roundLineString(geometry.coordinates); break;\n    default: throw \"unknown type: \" + geometry.type;\n  }\n  return geometry;\n}\n\nfunction roundMultiPolygon(coordinates) {\n  return coordinates.map(roundPolygon);\n}\n\nfunction roundPolygon(coordinates) {\n  return coordinates.map(roundLineString);\n}\n\nfunction roundLineString(coordinates) {\n  return coordinates.map(roundCoordinate);\n}\n\nfunction roundCoordinate(coordinate) {\n  return coordinate.map(round);\n}\n\nfunction round(x) {\n  return Math.round(x * roundPrecision) / roundPrecision;\n}\n\nfunction stringify(o) {\n  return JSON.stringify(o);\n}\n","encoding":"utf-8"}},"public":true,"created_at":"2012-09-09T06:30:04Z","updated_at":"2015-10-10T11:28:06Z","description":"Clean Up for Natural Earth GeoJSON","comments":0,"user":null,"comments_enabled":true,"comments_url":"https://api.github.com/gists/3682990/comments","owner":{"login":"mbostock","id":230541,"node_id":"MDQ6VXNlcjIzMDU0MQ==","avatar_url":"https://avatars.githubusercontent.com/u/230541?v=4","gravatar_id":"","url":"https://api.github.com/users/mbostock","html_url":"https://github.com/mbostock","followers_url":"https://api.github.com/users/mbostock/followers","following_url":"https://api.github.com/users/mbostock/following{/other_user}","gists_url":"https://api.github.com/users/mbostock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbostock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbostock/subscriptions","organizations_url":"https://api.github.com/users/mbostock/orgs","repos_url":"https://api.github.com/users/mbostock/repos","events_url":"https://api.github.com/users/mbostock/events{/privacy}","received_events_url":"https://api.github.com/users/mbostock/received_events","type":"User","user_view_type":"public","site_admin":false},"forks":[{"url":"https://api.github.com/gists/7efad33b6b5f59b0e4c3","user":{"login":"danieledler","id":7313550,"node_id":"MDQ6VXNlcjczMTM1NTA=","avatar_url":"https://avatars.githubusercontent.com/u/7313550?v=4","gravatar_id":"","url":"https://api.github.com/users/danieledler","html_url":"https://github.com/danieledler","followers_url":"https://api.github.com/users/danieledler/followers","following_url":"https://api.github.com/users/danieledler/following{/other_user}","gists_url":"https://api.github.com/users/danieledler/gists{/gist_id}","starred_url":"https://api.github.com/users/danieledler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/danieledler/subscriptions","organizations_url":"https://api.github.com/users/danieledler/orgs","repos_url":"https://api.github.com/users/danieledler/repos","events_url":"https://api.github.com/users/danieledler/events{/privacy}","received_events_url":"https://api.github.com/users/danieledler/received_events","type":"User","user_view_type":"public","site_admin":false,"name":"Daniel Edler","company":null,"blog":"","location":"Sweden","email":null,"hireable":null,"bio":null,"twitter_username":null,"public_repos":46,"public_gists":5,"followers":19,"following":0,"created_at":"2014-04-16T10:44:08Z","updated_at":"2026-04-17T10:13:20Z"},"id":"7efad33b6b5f59b0e4c3","created_at":"2015-05-07T18:30:10Z","updated_at":"2015-08-29T14:20:46Z"}],"history":[{"user":{"login":"mbostock","id":230541,"node_id":"MDQ6VXNlcjIzMDU0MQ==","avatar_url":"https://avatars.githubusercontent.com/u/230541?v=4","gravatar_id":"","url":"https://api.github.com/users/mbostock","html_url":"https://github.com/mbostock","followers_url":"https://api.github.com/users/mbostock/followers","following_url":"https://api.github.com/users/mbostock/following{/other_user}","gists_url":"https://api.github.com/users/mbostock/gists{/gist_id}","starred_url":"https://api.github.com/users/mbostock/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mbostock/subscriptions","organizations_url":"https://api.github.com/users/mbostock/orgs","repos_url":"https://api.github.com/users/mbostock/repos","events_url":"https://api.github.com/users/mbostock/events{/privacy}","received_events_url":"https://api.github.com/users/mbostock/received_events","type":"User","user_view_type":"public","site_admin":false},"version":"23194de63e5d6d4129f171c7d640ceb89eee1847","committed_at":"2012-09-09T06:30:04Z","change_status":{"total":77,"additions":77,"deletions":0},"url":"https://api.github.com/gists/3682990/23194de63e5d6d4129f171c7d640ceb89eee1847"}],"truncated":false}