MCPcopy Create free account
hub / github.com/plotly/plotly.js / feature2polygons

Function feature2polygons

src/lib/geo_location_utils.js:104–196  ·  view source on GitHub ↗
(feature)

Source from the content-addressed store, hash-verified

102}
103
104function feature2polygons(feature) {
105 var geometry = feature.geometry;
106 var coords = geometry.coordinates;
107 var loc = feature.id;
108
109 var polygons = [];
110 var appendPolygon, j, k, m;
111
112 if (loc === 'RUS' || loc === 'FJI') {
113 // Russia and Fiji have landmasses that cross the antimeridian,
114 // we need to add +360 to their longitude coordinates, so that
115 // polygon 'contains' doesn't get confused when crossing the antimeridian.
116 //
117 // Note that other countries have polygons on either side of the antimeridian
118 // (e.g. some Aleutian island for the USA), but those don't confuse
119 // the 'contains' method; these are skipped here.
120 appendPolygon = function (_pts) {
121 var pts;
122
123 if (doesCrossAntiMeridian(_pts) === null) {
124 pts = _pts;
125 } else {
126 pts = new Array(_pts.length);
127 for (m = 0; m < _pts.length; m++) {
128 // do not mutate calcdata[i][j].geojson !!
129 pts[m] = [_pts[m][0] < 0 ? _pts[m][0] + ANTIMERIDIAN_LON_SHIFT : _pts[m][0], _pts[m][1]];
130 }
131 }
132
133 polygons.push(polygon.tester(pts));
134 };
135 } else if (loc === 'ATA') {
136 // Antarctica has a landmass that wraps around every longitudes which
137 // confuses the 'contains' methods.
138 appendPolygon = function (pts) {
139 var crossAntiMeridianIndex = doesCrossAntiMeridian(pts);
140
141 // polygon that do not cross anti-meridian need no special handling
142 if (crossAntiMeridianIndex === null) {
143 return polygons.push(polygon.tester(pts));
144 }
145
146 // stitch polygon by adding pt over South Pole,
147 // so that it covers the projected region covers all latitudes
148 //
149 // Note that the algorithm below only works for polygons that
150 // start and end on longitude -180 (like the ones built by
151 // https://github.com/etpinard/sane-topojson).
152 var stitch = new Array(pts.length + 1);
153 var si = 0;
154
155 for (m = 0; m < pts.length; m++) {
156 if (m > crossAntiMeridianIndex) {
157 stitch[si++] = [pts[m][0] + ANTIMERIDIAN_LON_SHIFT, pts[m][1]];
158 } else if (m === crossAntiMeridianIndex) {
159 stitch[si++] = pts[m];
160 stitch[si++] = [pts[m][0], -90];
161 } else {

Callers

nothing calls this directly

Calls 1

doesCrossAntiMeridianFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…