Skip to content

Commit

Permalink
added shp -> mongodb importer
Browse files Browse the repository at this point in the history
  • Loading branch information
hamer committed Sep 20, 2013
1 parent e7f538e commit b581233
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
*.o
/mongodb.input
node_modules
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ In another terminal:
cd db
mongod --dbpath .

3) Import a shapefile
3) Prepare test environment

cd test/
npm install mongodb mapnik

4) Import a shapefile

In another terminal:

cd test/
python import.py
node import.js

4) Run test.py
5) Run test.js

cd test/
python test.py
node test.js


# Trouble
Expand Down
55 changes: 55 additions & 0 deletions test/import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

var path = require("path"), fs = require("fs");
var mapnik = require("mapnik"), mongodb = require("mongodb");

var server = new mongodb.Server("localhost", 27017),
connector = new mongodb.Db("gis", server, { w: 1 });

var nReady = 0;

connector.open(function(err, db) {
if (err)
return console.log("mongodb connect error:", err.message);

[ "points", "linestrings", "polygons" ].forEach(function(name) {
db.collection(name, function(err, collection) {
if (err) {
connector.close();
return console.log("collection selecting error:", err.message);
}

collection.ensureIndex({ loc: "2dsphere" }, function(err) {
if (err) {
connector.close();
return console.log("collection selecting error:", err.message);
}

importShp(name, collection);
});
});
});
});

function importShp(name, collection) {
var dataSource = new mapnik.Datasource({
type: "shape",
file: path.join("shp", name)
}),
featureSet = dataSource.featureset(), feature;

(function next(feature) {
if (!feature) {
if (++nReady === 3) {
console.log("done...");
connector.close();
}

return;
}

var json = JSON.parse(feature.toJSON()), doc = json.properties;
doc.loc = json.geometry;
collection.insert(doc, function(err) { next(featureSet.next()); });
})(featureSet.next());
}
Binary file added test/shp/linestrings.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions test/shp/linestrings.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions test/shp/linestrings.qpj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
Binary file added test/shp/linestrings.shp
Binary file not shown.
Binary file added test/shp/linestrings.shx
Binary file not shown.
Binary file added test/shp/points.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions test/shp/points.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions test/shp/points.qpj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
Binary file added test/shp/points.shp
Binary file not shown.
Binary file added test/shp/points.shx
Binary file not shown.
Binary file added test/shp/polygons.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions test/shp/polygons.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
1 change: 1 addition & 0 deletions test/shp/polygons.qpj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]
Binary file added test/shp/polygons.shp
Binary file not shown.
Binary file added test/shp/polygons.shx
Binary file not shown.

0 comments on commit b581233

Please sign in to comment.