Skip to content
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 coordinates to WGS 84 base on the .prj file #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions bin/shp2json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var fs = require("fs"),
commander = require("commander"),
shapefile = require("../");
shapefile = require("../"),
proj4 = require('proj4');

commander
.version(require("../package.json").version)
Expand All @@ -26,10 +27,18 @@ else if (commander.args.length !== 1) {

var out = (commander.out === "-" ? process.stdout : fs.createWriteStream(commander.out)).on("error", handleEpipe);

// My addition to read prj file, turn it into a function that I will apply to all coordinates
var shpFilename = commander.args[0],
prjFilename = shpFilename.substring(0, shpFilename.length - 4) + ".prj",
prjStr = fs.readFileSync(prjFilename).toString(),
prj = proj4(prjStr),
xform = prj.inverse.bind(prj);
// end for now, but `xform` will be passed around

shapefile.open(
commander.args[0] === "-" ? process.stdin : commander.args[0],
commander.geometry || commander.ignoreProperties ? null : undefined,
{encoding: commander.encoding})
{encoding: commander.encoding, xform: xform})
.then(commander.newlineDelimited
? (commander.geometry ? writeNewlineDelimitedGeometries : writeNewlineDelimitedFeatures)
: (commander.geometry ? writeGeometryCollection : writeFeatureCollection))
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function open(shp, dbf, options) {
return Promise.all([shp, dbf]).then(function(sources) {
var shp = sources[0], dbf = sources[1], encoding = "windows-1252";
if (options && options.encoding != null) encoding = options.encoding;
return shapefile(shp, dbf, dbf && new TextDecoder(encoding));
return shapefile(shp, dbf, dbf && new TextDecoder(encoding), options ? options.xform : function (x) {x});
});
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@
"array-source": "0.0",
"commander": "2",
"path-source": "0.1",
"proj4": "2.7.0",
"slice-source": "0.4",
"stream-source": "0.3",
"text-encoding": "^0.6.4"
},
"devDependencies": {
"package-preamble": "0.1",
"rollup": "0.49",
"rollup": "^0.49.3",
"rollup-plugin-node-resolve": "3",
"tape": "4",
"uglify-js": "3"
Expand Down
4 changes: 2 additions & 2 deletions shapefile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import shp from "../shp/index";
import shapefile_cancel from "./cancel";
import shapefile_read from "./read";

export default function(shpSource, dbfSource, decoder) {
export default function(shpSource, dbfSource, decoder, xform) {
return Promise.all([
shp(shpSource),
shp(shpSource, xform),
dbfSource && dbf(dbfSource, decoder)
]).then(function(sources) {
return new Shapefile(sources[0], sources[1]);
Expand Down
11 changes: 7 additions & 4 deletions shp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ var parsers = {
28: parseMultiPoint // MultiPointM
};

export default function(source) {
export default function(source, xform) {
source = slice(source);
return source.slice(100).then(function(array) {
return new Shp(source, view(array));
return new Shp(source, view(array), xform);
});
};

function Shp(source, header) {
function Shp(source, header, xform) {
var type = header.getInt32(32, true);
if (!(type in parsers)) throw new Error("unsupported shape type: " + type);
this._source = source;
this._xform = xform || function (x) { x };
this._type = type;
this._index = 0;
this._parse = parsers[type];
this.bbox = [header.getFloat64(36, true), header.getFloat64(44, true), header.getFloat64(52, true), header.getFloat64(60, true)];
var topLeft = xform([header.getFloat64(36, true), header.getFloat64(44, true)]);
var botRight = xform([header.getFloat64(52, true), header.getFloat64(60, true)]);
this.bbox = [ topLeft[0], topLeft[1], botRight[0], botRight[1] ];
}

var prototype = Shp.prototype;
Expand Down
6 changes: 4 additions & 2 deletions shp/multipoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default function(record) {
export default function(record, xform) {
var i = 40, j, n = record.getInt32(36, true), coordinates = new Array(n);
for (j = 0; j < n; ++j, i += 16) coordinates[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
for (j = 0; j < n; ++j, i += 16) {
coordinates[j] = xform([ record.getFloat64(i, true), record.getFloat64(i + 8, true) ]);
}
return {type: "MultiPoint", coordinates: coordinates};
};
4 changes: 2 additions & 2 deletions shp/point.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function(record) {
return {type: "Point", coordinates: [record.getFloat64(4, true), record.getFloat64(12, true)]};
export default function(record, xform) {
return {type: "Point", coordinates: xform([ record.getFloat64(4, true), record.getFloat64(12, true) ])};
};
7 changes: 4 additions & 3 deletions shp/polygon.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default function(record) {
var i = 44, j, n = record.getInt32(36, true), m = record.getInt32(40, true), parts = new Array(n), points = new Array(m), polygons = [], holes = [];
export default function(record, xform) {
var i = 44, j, n = record.getInt32(36, true), m = record.getInt32(40, true),
parts = new Array(n), points = new Array(m), polygons = [], holes = [];
for (j = 0; j < n; ++j, i += 4) parts[j] = record.getInt32(i, true);
for (j = 0; j < m; ++j, i += 16) points[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
for (j = 0; j < m; ++j, i += 16) points[j] = xform([ record.getFloat64(i, true), record.getFloat64(i + 8, true) ]);

parts.forEach(function(i, j) {
var ring = points.slice(i, parts[j + 1]);
Expand Down
4 changes: 2 additions & 2 deletions shp/polyline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function(record) {
export default function(record, xform) {
var i = 44, j, n = record.getInt32(36, true), m = record.getInt32(40, true), parts = new Array(n), points = new Array(m);
for (j = 0; j < n; ++j, i += 4) parts[j] = record.getInt32(i, true);
for (j = 0; j < m; ++j, i += 16) points[j] = [record.getFloat64(i, true), record.getFloat64(i + 8, true)];
for (j = 0; j < m; ++j, i += 16) points[j] = xform([ record.getFloat64(i, true), record.getFloat64(i + 8, true)]);
return n === 1
? {type: "LineString", coordinates: points}
: {type: "MultiLineString", coordinates: parts.map(function(i, j) { return points.slice(i, parts[j + 1]); })};
Expand Down
2 changes: 1 addition & 1 deletion shp/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function() {
function read() {
var length = header.getInt32(4, false) * 2 - 4, type = header.getInt32(8, true);
return length < 0 || (type && type !== that._type) ? skip() : that._source.slice(length).then(function(chunk) {
return {done: false, value: type ? that._parse(view(concat(array.slice(8), chunk))) : null};
return {done: false, value: type ? that._parse(view(concat(array.slice(8), chunk)), that._xform) : null};
});
}

Expand Down