Debian Codegraph @

topojson/src/feature.js

raw
1 import {absolute as transformAbsolute} from "./transform";
2 import {reverse} from "./array";
3
4 export default function(topology, o) {
5 return o.type === "GeometryCollection" ? {
6 type: "FeatureCollection",
7 features: o.geometries.map(function(o) { return feature(topology, o); })
8 } : feature(topology, o);
9 }
10
11 export function feature(topology, o) {
12 var f = {
13 type: "Feature",
14 id: o.id,
15 properties: o.properties || {},
16 geometry: object(topology, o)
17 };
18 if (o.id == null) delete f.id;
19 return f;
20 }
21
22 export function object(topology, o) {
23 var absolute = transformAbsolute(topology.transform),
24 arcs = topology.arcs;
25
26 function arc(i, points) {
27 if (points.length) points.pop();
28 for (var a = arcs[i < 0 ? ~i : i], k = 0, n = a.length, p; k < n; ++k) {
29 points.push(p = a[k].slice());
30 absolute(p, k);
31 }
32 if (i < 0) reverse(points, n);
33 }
34
35 function point(p) {
36 p = p.slice();
37 absolute(p, 0);
38 return p;
39 }
40
41 function line(arcs) {
42 var points = [];
43 for (var i = 0, n = arcs.length; i < n; ++i) arc(arcs[i], points);
44 if (points.length < 2) points.push(points[0].slice());
45 return points;
46 }
47
48 function ring(arcs) {
49 var points = line(arcs);
50 while (points.length < 4) points.push(points[0].slice());
51 return points;
52 }
53
54 function polygon(arcs) {
55 return arcs.map(ring);
56 }
57
58 function geometry(o) {
59 var t = o.type;
60 return t === "GeometryCollection" ? {type: t, geometries: o.geometries.map(geometry)}
61 : t in geometryType ? {type: t, coordinates: geometryType[t](o)}
62 : null;
63 }
64
65 var geometryType = {
66 Point: function(o) { return point(o.coordinates); },
67 MultiPoint: function(o) { return o.coordinates.map(point); },
68 LineString: function(o) { return line(o.arcs); },
69 MultiLineString: function(o) { return o.arcs.map(line); },
70 Polygon: function(o) { return polygon(o.arcs); },
71 MultiPolygon: function(o) { return o.arcs.map(polygon); }
72 };
73
74 return geometry(o);
75 }