-
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
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
e483044
convert KML and GPX to GeoJSON
wilhelmberg a8d1df1
GPX: drop 'track_points' and 'route_points'
wilhelmberg 5ab4de5
add metadata and index file to new bundle dirs, still troubleshooting…
5e1d36b
split out kml togeojson tests and moved indexing
677a427
flush and close datasets before mapnik-indexing them
wilhelmberg 4f96667
togeojson: release objects to be able to index
wilhelmberg 43d2630
[wip] make tests work again
wilhelmberg 1952055
split out tests
8dc4291
manually release gdal's reference to dataset
wilhelmberg abf63bc
fix mbtiles test
wilhelmberg 44fc555
fix GPX test
wilhelmberg c9ca030
fix kml test
wilhelmberg fa90501
add 10MB filter for indexing kml/gpx layers
a08b2ca
unlink 2 tmp files of test/spatial-index.test.js
wilhelmberg d07f007
travis?
f781e3c
bump mapnik-omnivore to include newest node-mapnik
cd5474e
node-mapnik doesnt support builds for node v0.12
a61209a
re-add special character layer test, it got left behind
d6af91f
travis? delete temp dirs of `spatial-index.test.js`
wilhelmberg fc21183
WIP not yet finished, clean up temporary test data
wilhelmberg 2a20b12
oops, style errors
wilhelmberg 338d5a2
clean up temp data after 'npm test'
wilhelmberg 5aad818
what is data?
4cc9d80
`Merge branch 'master' into togeojson
197766e
remove unneeded log
18c3a26
udpate mapnik-om
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
preprocessors/test* | ||
test/fixtures/*.aux.xml |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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') { | ||
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); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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 comment
The 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