linestring.js
4.84 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module.exports = LineString;
var util = require('util');
var Geometry = require('./geometry');
var Types = require('./types');
var Point = require('./point');
var BinaryWriter = require('./binarywriter');
function LineString(points, srid) {
Geometry.call(this);
this.points = points || [];
this.srid = srid;
if (this.points.length > 0) {
this.hasZ = this.points[0].hasZ;
this.hasM = this.points[0].hasM;
}
}
util.inherits(LineString, Geometry);
LineString.Z = function (points, srid) {
var lineString = new LineString(points, srid);
lineString.hasZ = true;
return lineString;
};
LineString.M = function (points, srid) {
var lineString = new LineString(points, srid);
lineString.hasM = true;
return lineString;
};
LineString.ZM = function (points, srid) {
var lineString = new LineString(points, srid);
lineString.hasZ = true;
lineString.hasM = true;
return lineString;
};
LineString._parseWkt = function (value, options) {
var lineString = new LineString();
lineString.srid = options.srid;
lineString.hasZ = options.hasZ;
lineString.hasM = options.hasM;
if (value.isMatch(['EMPTY']))
return lineString;
value.expectGroupStart();
lineString.points.push.apply(lineString.points, value.matchCoordinates(options));
value.expectGroupEnd();
return lineString;
};
LineString._parseWkb = function (value, options) {
var lineString = new LineString();
lineString.srid = options.srid;
lineString.hasZ = options.hasZ;
lineString.hasM = options.hasM;
var pointCount = value.readUInt32();
for (var i = 0; i < pointCount; i++)
lineString.points.push(Point._readWkbPoint(value, options));
return lineString;
};
LineString._parseTwkb = function (value, options) {
var lineString = new LineString();
lineString.hasZ = options.hasZ;
lineString.hasM = options.hasM;
if (options.isEmpty)
return lineString;
var previousPoint = new Point(0, 0, options.hasZ ? 0 : undefined, options.hasM ? 0 : undefined);
var pointCount = value.readVarInt();
for (var i = 0; i < pointCount; i++)
lineString.points.push(Point._readTwkbPoint(value, options, previousPoint));
return lineString;
};
LineString._parseGeoJSON = function (value) {
var lineString = new LineString();
if (value.coordinates.length > 0)
lineString.hasZ = value.coordinates[0].length > 2;
for (var i = 0; i < value.coordinates.length; i++)
lineString.points.push(Point._readGeoJSONPoint(value.coordinates[i]));
return lineString;
};
LineString.prototype.toWkt = function () {
if (this.points.length === 0)
return this._getWktType(Types.wkt.LineString, true);
return this._getWktType(Types.wkt.LineString, false) + this._toInnerWkt();
};
LineString.prototype._toInnerWkt = function () {
var innerWkt = '(';
for (var i = 0; i < this.points.length; i++)
innerWkt += this._getWktCoordinate(this.points[i]) + ',';
innerWkt = innerWkt.slice(0, -1);
innerWkt += ')';
return innerWkt;
};
LineString.prototype.toWkb = function (parentOptions) {
var wkb = new BinaryWriter(this._getWkbSize());
wkb.writeInt8(1);
this._writeWkbType(wkb, Types.wkb.LineString, parentOptions);
wkb.writeUInt32LE(this.points.length);
for (var i = 0; i < this.points.length; i++)
this.points[i]._writeWkbPoint(wkb);
return wkb.buffer;
};
LineString.prototype.toTwkb = function () {
var twkb = new BinaryWriter(0, true);
var precision = Geometry.getTwkbPrecision(5, 0, 0);
var isEmpty = this.points.length === 0;
this._writeTwkbHeader(twkb, Types.wkb.LineString, precision, isEmpty);
if (this.points.length > 0) {
twkb.writeVarInt(this.points.length);
var previousPoint = new Point(0, 0, 0, 0);
for (var i = 0; i < this.points.length; i++)
this.points[i]._writeTwkbPoint(twkb, precision, previousPoint);
}
return twkb.buffer;
};
LineString.prototype._getWkbSize = function () {
var coordinateSize = 16;
if (this.hasZ)
coordinateSize += 8;
if (this.hasM)
coordinateSize += 8;
return 1 + 4 + 4 + (this.points.length * coordinateSize);
};
LineString.prototype.toGeoJSON = function (options) {
var geoJSON = Geometry.prototype.toGeoJSON.call(this, options);
geoJSON.type = Types.geoJSON.LineString;
geoJSON.coordinates = [];
for (var i = 0; i < this.points.length; i++) {
if (this.hasZ)
geoJSON.coordinates.push([this.points[i].x, this.points[i].y, this.points[i].z]);
else
geoJSON.coordinates.push([this.points[i].x, this.points[i].y]);
}
return geoJSON;
};