Created
November 17, 2015 20:34
-
-
Save williamho/9b638bd5cddf75877eef to your computer and use it in GitHub Desktop.
convert marineregions geojson to mongodb 2.4 mongoimport compatible file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Convert marineregions json (converted from shapefile) to mongodb 2.4 compatible format (for mongoimport) | |
| Mongo 2.4 doesn't support `MultiPolygon` type, so split `MultiPolygon` into multiple individual `Polygon`s. | |
| Also convert ISO alpha 3 to alpha 2. | |
| Usage: | |
| Convert the marineregions shapefile (EEZ_land_v2_201410.shp) to a geojson file: | |
| ogr2ogr -f GeoJSON -t_srs crs:84 -clipsrc -180 -90 180 90 countries.geojson *.shp | |
| Then use jq to extract the features, one per line, and run this node script on it: | |
| jq -c '.features[]' countries.geojson | node index.js >mongoimport_out.txt | |
| Then run mongoimport as usual: | |
| mongoimport -h 127.0.0.1 -d location -c countryBoundaries <mongoimport_out.txt | |
| You should also add a 2dsphere index (example using mongo shell): | |
| mongo --host 127.0.0.1 | |
| > use location | |
| > db.countryBoundaries.createIndex({"geometry":"2dsphere"}) | |
| */ | |
| var split = require('split'); | |
| function main() { | |
| process.stdin | |
| .pipe(split(JSON.parse)) | |
| .on('error', function (err) { | |
| console.error(err); | |
| }) | |
| .on('data', function(feature) { | |
| var countryCode = countryMapping[feature.properties.ISO_3digit]; | |
| if (countryCode === undefined) { return; } // Ignore countries with invalid ISO alpha 2 | |
| var polygons = []; | |
| if (feature.geometry.type == 'MultiPolygon') { | |
| var polys = feature.geometry.coordinates.map(function(coords) { | |
| return { type: 'Polygon', coordinates: coords }; | |
| }); | |
| polygons = polygons.concat(polys); | |
| } else { | |
| polygons.push(feature.geometry); | |
| } | |
| polygons.forEach(function(polygon) { | |
| var doc = { | |
| countryCode: countryCode, | |
| name: feature.properties.Country, | |
| geometry: polygon | |
| }; | |
| console.log(JSON.stringify(doc)); | |
| }); | |
| }) | |
| } | |
| var countryMapping = { | |
| "AFG": "AF", | |
| "ALA": "AX", | |
| "ALB": "AL", | |
| "DZA": "DZ", | |
| "ASM": "AS", | |
| "AND": "AD", | |
| "AGO": "AO", | |
| "AIA": "AI", | |
| "ATA": "AQ", | |
| "ATG": "AG", | |
| "ARG": "AR", | |
| "ARM": "AM", | |
| "ABW": "AW", | |
| "AUS": "AU", | |
| "AUT": "AT", | |
| "AZE": "AZ", | |
| "BHS": "BS", | |
| "BHR": "BH", | |
| "BGD": "BD", | |
| "BRB": "BB", | |
| "BLR": "BY", | |
| "BEL": "BE", | |
| "BLZ": "BZ", | |
| "BEN": "BJ", | |
| "BMU": "BM", | |
| "BTN": "BT", | |
| "BOL": "BO", | |
| "BES": "BQ", | |
| "BIH": "BA", | |
| "BWA": "BW", | |
| "BVT": "BV", | |
| "BRA": "BR", | |
| "IOT": "IO", | |
| "BRN": "BN", | |
| "BGR": "BG", | |
| "BFA": "BF", | |
| "BDI": "BI", | |
| "KHM": "KH", | |
| "CMR": "CM", | |
| "CAN": "CA", | |
| "CPV": "CV", | |
| "CYM": "KY", | |
| "CAF": "CF", | |
| "TCD": "TD", | |
| "CHL": "CL", | |
| "CHN": "CN", | |
| "CXR": "CX", | |
| "CCK": "CC", | |
| "COL": "CO", | |
| "COM": "KM", | |
| "COG": "CG", | |
| "COD": "CD", | |
| "COK": "CK", | |
| "CRI": "CR", | |
| "CIV": "CI", | |
| "HRV": "HR", | |
| "CUB": "CU", | |
| "CUW": "CW", | |
| "CW": "CW", // marineregions data uses CW instead of CUW for the 3-digit code | |
| "CYP": "CY", | |
| "CZE": "CZ", | |
| "DNK": "DK", | |
| "DJI": "DJ", | |
| "DMA": "DM", | |
| "DOM": "DO", | |
| "ECU": "EC", | |
| "EGY": "EG", | |
| "SLV": "SV", | |
| "SSD": "SS", // South Sudan | |
| "GNQ": "GQ", | |
| "ERI": "ER", | |
| "EST": "EE", | |
| "ETH": "ET", | |
| "FLK": "FK", | |
| "FRO": "FO", | |
| "FJI": "FJ", | |
| "FIN": "FI", | |
| "FRA": "FR", | |
| "GUF": "GF", | |
| "PYF": "PF", | |
| "ATF": "TF", | |
| "GAB": "GA", | |
| "GMB": "GM", | |
| "GEO": "GE", | |
| "DEU": "DE", | |
| "GHA": "GH", | |
| "GIB": "GI", | |
| "GRC": "GR", | |
| "GRL": "GL", | |
| "GRD": "GD", | |
| "GLP": "GP", | |
| "GUM": "GU", | |
| "GTM": "GT", | |
| "GGY": "GG", | |
| "GIN": "GN", | |
| "GNB": "GW", | |
| "GUY": "GY", | |
| "HTI": "HT", | |
| "HMD": "HM", | |
| "VAT": "VA", | |
| "VTC": "VA", // VTC instead of VAT in marineregions data | |
| "HND": "HN", | |
| "HKG": "HK", | |
| "HUN": "HU", | |
| "ISL": "IS", | |
| "IND": "IN", | |
| "IDN": "ID", | |
| "IRN": "IR", | |
| "IRQ": "IQ", | |
| "IRL": "IE", | |
| "IMN": "IM", | |
| "ISR": "IL", | |
| "ITA": "IT", | |
| "JAM": "JM", | |
| "JPN": "JP", | |
| "JEY": "JE", | |
| "JOR": "JO", | |
| "KAZ": "KZ", | |
| "KEN": "KE", | |
| "KIR": "KI", | |
| "PRK": "KP", | |
| "KOR": "KR", | |
| "KWT": "KW", | |
| "KGZ": "KG", | |
| "LAO": "LA", | |
| "LVA": "LV", | |
| "LBN": "LB", | |
| "LSO": "LS", | |
| "LBR": "LR", | |
| "LBY": "LY", | |
| "LIE": "LI", | |
| "LTU": "LT", | |
| "LUX": "LU", | |
| "MAC": "MO", | |
| "MKD": "MK", | |
| "MDG": "MG", | |
| "MWI": "MW", | |
| "MYS": "MY", | |
| "MDV": "MV", | |
| "MLI": "ML", | |
| "MLT": "MT", | |
| "MHL": "MH", | |
| "MTQ": "MQ", | |
| "MRT": "MR", | |
| "MUS": "MU", | |
| "MYT": "YT", | |
| "MEX": "MX", | |
| "FSM": "FM", | |
| "MDA": "MD", | |
| "MCO": "MC", | |
| "MNG": "MN", | |
| "MNE": "ME", | |
| "MSR": "MS", | |
| "MAR": "MA", | |
| "MOZ": "MZ", | |
| "MMR": "MM", | |
| "NAM": "NA", | |
| "NRU": "NR", | |
| "NPL": "NP", | |
| "NLD": "NL", | |
| "NCL": "NC", | |
| "NZL": "NZ", | |
| "NIC": "NI", | |
| "NER": "NE", | |
| "NGA": "NG", | |
| "NIU": "NU", | |
| "NFK": "NF", | |
| "MNP": "MP", | |
| "MNP++": "MP", // MNP++ in the marineregions data for some reason | |
| "NOR": "NO", | |
| "PSE": "PS", | |
| "OMN": "OM", | |
| "PAK": "PK", | |
| "PLW": "PW", | |
| "PAN": "PA", | |
| "PNG": "PG", | |
| "PRY": "PY", | |
| "PER": "PE", | |
| "PHL": "PH", | |
| "PCN": "PN", | |
| "POL": "PL", | |
| "PRT": "PT", | |
| "PRI": "PR", | |
| "QAT": "QA", | |
| "REU": "RE", | |
| "ROU": "RO", | |
| "RUS": "RU", | |
| "RWA": "RW", | |
| "BLM": "BL", | |
| "SHN": "SH", | |
| "KNA": "KN", | |
| "LCA": "LC", | |
| "MAF": "MF", | |
| "SPM": "PM", | |
| "VCT": "VC", | |
| "WSM": "WS", | |
| "SMR": "SM", | |
| "STP": "ST", | |
| "SAU": "SA", | |
| "SEN": "SN", | |
| "SRB": "RS", | |
| "SYC": "SC", | |
| "SLE": "SL", | |
| "SGP": "SG", | |
| "SXM": "SX", | |
| "SVK": "SK", | |
| "SVN": "SI", | |
| "SLB": "SB", | |
| "SOM": "SO", | |
| "ZAF": "ZA", | |
| "SGS": "GS", | |
| "ESP": "ES", | |
| "LKA": "LK", | |
| "SDN": "SD", | |
| "SUR": "SR", | |
| "SJM": "SJ", | |
| "SWZ": "SZ", | |
| "SWE": "SE", | |
| "CHE": "CH", | |
| "SYR": "SY", | |
| "TWN": "TW", | |
| "TJK": "TJ", | |
| "TZA": "TZ", | |
| "THA": "TH", | |
| "TLS": "TL", | |
| "TGO": "TG", | |
| "TKL": "TK", | |
| "TON": "TO", | |
| "TTO": "TT", | |
| "TUN": "TN", | |
| "TUR": "TR", | |
| "TKM": "TM", | |
| "TCA": "TC", | |
| "TUV": "TV", | |
| "UGA": "UG", | |
| "UKR": "UA", | |
| "ARE": "AE", | |
| "GBR": "GB", | |
| "USA": "US", | |
| "UMI": "UM", | |
| "URY": "UY", | |
| "UZB": "UZ", | |
| "VUT": "VU", | |
| "VEN": "VE", | |
| "VNM": "VN", | |
| "VGB": "VG", | |
| "VIR": "VI", | |
| "WLF": "WF", | |
| "ESH": "EH", | |
| "YEM": "YE", | |
| "ZMB": "ZM", | |
| "ZWE": "ZW" | |
| }; | |
| main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "marineregions-geojson-formatter", | |
| "version": "1.0.0", | |
| "description": "Convert marineregions geojson to use ISO alpha 2 country codes and split multipolygons", | |
| "main": "index.js", | |
| "author": "", | |
| "license": "MIT", | |
| "dependencies": { | |
| "split": "^1.0.0" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment