forked from yuletide/node-shp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
119 lines (109 loc) · 3.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var Dbf = require(__dirname + '/lib/dbf'),
Shp = require(__dirname + '/lib/shp');
/** getOpenLayersFeatures
* @param {String} url - the base url for the shapefile, without extensions
* @param {Function} callback Called with an array of OpenLayers.Feature.Vector for the given URL
*/
//getFeatures("demo/TM_WORLD_BORDERS_SIMPL/TM_WORLD_BORDERS_SIMPL-0.3", function(err, data){
// console.log(JSON.stringify(data));
//});
// loads and parses shapefile, returns geojson
function getFeatures(url, callback) {
this.shpURL = url+'.shp';
this.dbfURL = url+'.dbf';
this.callback = callback;
var instance = this;
this.json = {
type: "FeatureCollection",
features: []
};
try {
this.shpFile = new Shp(url);
this.dbfFile = new Dbf(url);
} catch(error) {
console.error("Error parsing shapefile");
if (callback){
callback(error, null);
} else {
throw new Error(error);
}
}
var recordsLength = instance.shpFile.records.length;
for (var i = 0; i < recordsLength; i++) {
var record = instance.shpFile.records[i];
var attrs = instance.dbfFile.records[i];
var feature = {
type: "Feature",
geometry: {},
properties: {}
};
// console.log(record);
feature.properties = attrs.values;
switch(record.shapeType) {
case Shp.ShpType.SHAPE_POINT:
feature.geometry.type = Shp.GeoJsonType[record.shapeType];
feature.geometry.coordinates = [record.shape.x, record.shape.y];
break;
case Shp.ShpType.SHAPE_POINTZ:
feature.geometry.type = Shp.GeoJsonType[record.shapeType];
feature.geometry.coordinates = [record.shape.x, record.shape.y,record.shape.z];
break;
case Shp.ShpType.SHAPE_POLYLINE:
feature.geometry.type = Shp.GeoJsonType[record.shapeType];
feature.geometry.coordinates = [];
var pointsLen = record.shape.rings[0].length;
for (var j = 0; j < pointsLen; j++) {
feature.geometry.coordinates.push([record.shape.rings[0][j].x, record.shape.rings[0][j].y]);
}
break;
case Shp.ShpType.SHAPE_POLYLINEZ:
feature.geometry.type = Shp.GeoJsonType[record.shapeType];
feature.geometry.coordinates = [];
var pointsLen = record.shape.rings[0].length;
for (var j = 0; j < pointsLen; j++) {
feature.geometry.coordinates.push([record.shape.rings[0][j].x, record.shape.rings[0][j].y, record.shape.rings[0][j].z]);
}
break;
case Shp.ShpType.SHAPE_POLYGON:
feature.geometry.type = Shp.GeoJsonType[record.shapeType];
feature.geometry.coordinates = [];
var ringsLen = record.shape.rings.length;
for (var j = 0; j < ringsLen; j++) {
var ring = record.shape.rings[j];
if (ring.length < 1) continue;
var featureRing = feature.geometry.coordinates[j] = [];
var ringLen = ring.length;
for (var k = 0; k < ringLen; k++) {
featureRing.push([ring[k].x, ring[k].y]);
}
}
break;
case Shp.ShpType.SHAPE_POLYGONZ:
feature.geometry.type = Shp.GeoJsonType[record.shapeType];
feature.geometry.coordinates = [];
var ringsLen = record.shape.rings.length;
for (var j = 0; j < ringsLen; j++) {
var ring = record.shape.rings[j];
if (ring.length < 1) continue;
var featureRing = feature.geometry.coordinates[j] = [];
var ringLen = ring.length;
for (var k = 0; k < ringLen; k++) {
featureRing.push([ring[k].x, ring[k].y, ring[k].z]);
}
}
break;
default:
throw(new Error("Error converting SHP to geojson: Unsupported feature type"))
}
this.json.features.push(feature);
}
if (callback){
callback(null, this.json);
}
return this.json;
}
module.exports = {
readFileSync: getFeatures,
readFile: getFeatures
}
// ported from https://github.com/RandomEtc/shapefile-js/blob/master/src/ol_shapefile.js