-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert GPX/KML to geojson #61
Changes from 24 commits
e483044
a8d1df1
5ab4de5
5e1d36b
677a427
4f96667
43d2630
1952055
8dc4291
abf63bc
44fc555
c9ca030
fa90501
a08b2ca
d07f007
f781e3c
cd5474e
a61209a
d6af91f
fc21183
2a20b12
338d5a2
5aad818
4cc9d80
197766e
18c3a26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"rules": { | ||
"indent": [2, 2], | ||
"quotes": [2, "single"], | ||
"no-console": [0], | ||
"semi": [2, "always"] | ||
}, | ||
"env": { | ||
"node": true | ||
}, | ||
"globals": { | ||
"process": true, | ||
"module": true, | ||
"require": true | ||
}, | ||
"extends": "eslint:recommended" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
preprocessors/test* | ||
test/fixtures/*.aux.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
var gdal = require('gdal'); | ||
var fs = require('fs'); | ||
var mkdirp = require('mkdirp'); | ||
var queue = require('queue-async'); | ||
var spawn = require('child_process').spawn; | ||
var path = require('path'); | ||
var digest = require('mapnik-omnivore').digest; | ||
var mapnik = require('mapnik'); | ||
var mapnik_index = path.resolve(mapnik.module_path, 'mapnik-index' + (process.platform === 'win32' ? '.exe' : '')); | ||
if (!fs.existsSync(mapnik_index)) { | ||
throw new Error('mapnik-index does not exist at ' + mapnik_index); | ||
} | ||
|
||
//disable in production | ||
//gdal.verbose(); | ||
|
||
module.exports = function(infile, outdirectory, callback) { | ||
mkdirp(outdirectory, function(err) { | ||
if (err) return callback(err); | ||
|
||
var geojson_files = []; | ||
var ds_gpx; | ||
var full_feature_cnt = 0; | ||
var wgs84 = gdal.SpatialReference.fromEPSG(4326); | ||
|
||
try { | ||
ds_gpx = gdal.open(infile); | ||
} | ||
catch (err) { | ||
return callback(new Error(err)); | ||
} | ||
|
||
ds_gpx.layers.forEach(function(lyr_gpx) { | ||
//drop point layers as they can get really huge | ||
if (lyr_gpx.name === 'track_points' || lyr_gpx.name === 'route_points') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this represent a breaking change from present behavior? That is, would these layers be included in a GPX upload today? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they would. See discussion per https://github.com/mapbox/unpacker/issues/871#issuecomment-197206459 |
||
return; | ||
} | ||
|
||
var feat_cnt = lyr_gpx.features.count(true); | ||
if (feat_cnt === 0) { | ||
return; | ||
} | ||
|
||
var geojson; | ||
var lyr_name; | ||
var out_ds; | ||
var out_name; | ||
|
||
try { | ||
lyr_name = lyr_gpx.name; | ||
out_name = path.join(outdirectory, lyr_name + '.geojson'); | ||
out_ds = gdal.open(out_name, 'w', 'GeoJSON'); | ||
geojson = out_ds.layers.create(lyr_name, wgs84, lyr_gpx.geomType); | ||
} | ||
catch (err) { | ||
return callback(new Error(err)); | ||
} | ||
|
||
lyr_gpx.features.forEach(function(gpx_feat) { | ||
//skip null or empty geometries | ||
var geom = gpx_feat.getGeometry(); | ||
if (!geom) { | ||
return; | ||
} else { | ||
if (geom.isEmpty()) { | ||
return; | ||
} | ||
|
||
if (!geom.isValid()) { | ||
return; | ||
} | ||
} | ||
|
||
geojson.features.add(gpx_feat); | ||
full_feature_cnt++; | ||
}); | ||
|
||
geojson.flush(); | ||
out_ds.flush(); | ||
out_ds.close(); | ||
|
||
//release objects to be able to index | ||
geojson = null; | ||
out_ds = null; | ||
|
||
geojson_files.push(out_name); | ||
}); | ||
|
||
ds_gpx.close(); | ||
if (full_feature_cnt === 0) { | ||
return callback(new Error('GPX does not contain any valid features.')); | ||
} | ||
|
||
// Create metadata file for original gpx source | ||
var metadatafile = path.join(outdirectory, '/metadata.json'); | ||
digest(infile, function(err, metadata) { | ||
if (err) return callback(err); | ||
fs.writeFile(metadatafile, JSON.stringify(metadata), function(err) { | ||
if (err) return callback(err); | ||
return createIndices(callback); | ||
}); | ||
}); | ||
|
||
function createIndices(callback) { | ||
// create mapnik index for each geojson layer | ||
var q = queue(); | ||
geojson_files.forEach(function(gj) { | ||
q.defer(createIndex, gj); | ||
}); | ||
|
||
q.awaitAll(function(err) { | ||
if (err) return callback(err); | ||
return callback(); | ||
}); | ||
} | ||
|
||
function createIndex(layerfile, callback) { | ||
// Finally, create an .index file in the output dir (if layer is greater than index_worthy_size). | ||
// mapnik-index will automatically add ".index" to the end of the original filename | ||
fs.stat(layerfile, function(err, stats) { | ||
if (err) return callback(err); | ||
|
||
// check size is warrants creating an index | ||
if (stats.size >= module.exports.index_worthy_size) { | ||
var data = ''; | ||
var p = spawn(mapnik_index, [layerfile, '--validate-features']) | ||
.once('error', callback) | ||
.on('exit', function() { | ||
// If error printed to --validate-features log | ||
if (data.indexOf('Error') != -1) { | ||
return callback(data); | ||
} | ||
else return callback(); | ||
}); | ||
|
||
p.stderr.on('data', function(d) { | ||
d.toString(); | ||
data += d; | ||
}); | ||
} else { | ||
return callback(); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports.description = 'Convert GPX to GeoJSON'; | ||
module.exports.index_worthy_size = 10 * 1024 * 1024; // 10 MB | ||
|
||
module.exports.criteria = function(filepath, info, callback) { | ||
|
||
if (info.filetype !== 'gpx') return callback(null, false); | ||
|
||
callback(null, true); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debug logging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GretaCB what's the plan of attack?
This has changed here (https://github.com/mapbox/preprocessorcerer/compare/stderr-fix#diff-39694ce4a586caad3e6ad813c295b454R32) too as latest
mapnik-index
is now returning an error code, if--validate-features
fails.Need to wait to update to latest node-mapnik?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plan of attack re: mapbox/mapnik-swoop#14
cc @mapsam
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Working through a node-mapnik dev build so we can test this right now.