|
2016 | 2016 | function d3_geo_path_circle(radius) { |
2017 | 2017 | return "m0," + radius + "a" + radius + "," + radius + " 0 1,1 0," + -2 * radius + "a" + radius + "," + radius + " 0 1,1 0," + +2 * radius + "z"; |
2018 | 2018 | } |
2019 | | - function d3_geo_path_projectLine(projection) { |
2020 | | - return projection.line || function(lineString) { |
2021 | | - return [ lineString.map(projection) ]; |
2022 | | - }; |
2023 | | - } |
2024 | | - function d3_geo_path_projectPolygon(projection) { |
2025 | | - return projection.polygon || function(polygon) { |
2026 | | - return [ polygon.map(function(ring) { |
2027 | | - return ring.map(projection); |
2028 | | - }) ]; |
2029 | | - }; |
2030 | | - } |
2031 | 2019 | function d3_geo_projection(project) { |
2032 | 2020 | return d3_geo_projectionMutator(function() { |
2033 | 2021 | return project; |
|
2050 | 2038 | return p; |
2051 | 2039 | } |
2052 | 2040 | var project, projectRotate, k = 150, x = 480, y = 250, λ = 0, φ = 0, δλ = 0, δφ = 0, δγ = 0, δx = x, δy = y; |
| 2041 | + p.point = function(coordinates, context) { |
| 2042 | + var point = p(coordinates); |
| 2043 | + context.point(point[0], point[1]); |
| 2044 | + }; |
| 2045 | + p.line = function(coordinates, context) { |
| 2046 | + var point = p(coordinates[0]), i = 0, n = coordinates.length; |
| 2047 | + context.moveTo(point[0], point[1]); |
| 2048 | + while (++i < n) context.lineTo((point = p(coordinates[i]))[0], point[1]); |
| 2049 | + }; |
| 2050 | + p.ring = function(coordinates, context) { |
| 2051 | + p.line(coordinates, context); |
| 2052 | + context.closePath(); |
| 2053 | + }; |
2053 | 2054 | p.scale = function(_) { |
2054 | 2055 | if (!arguments.length) return k; |
2055 | 2056 | k = +_; |
|
6303 | 6304 | return d3_geo_projection(d3_geo_orthographic); |
6304 | 6305 | }; |
6305 | 6306 | d3.geo.path = function() { |
6306 | | - function path(d, i) { |
| 6307 | + function path(object) { |
6307 | 6308 | if (typeof pointRadius === "function") pointCircle = d3_geo_path_circle(pointRadius.apply(this, arguments)); |
6308 | | - pathType(d); |
6309 | | - var result = buffer.length ? buffer.join("") : null; |
6310 | | - buffer = []; |
6311 | | - return result; |
6312 | | - } |
6313 | | - function multiLineString(lineStrings) { |
6314 | | - for (var i = 0, n = lineStrings.length; i < n; ++i) { |
6315 | | - var lineString = lineStrings[i]; |
6316 | | - buffer.push("M"); |
6317 | | - for (var j = 0, m = lineString.length; j < m; ++j) buffer.push(lineString[j], "L"); |
6318 | | - buffer.pop(); |
6319 | | - } |
6320 | | - } |
6321 | | - function multiPolygon(polygons) { |
6322 | | - for (var i = 0, n = polygons.length; i < n; ++i) { |
6323 | | - var polygon = polygons[i]; |
6324 | | - for (var j = 0, m = polygon.length; j < m; ++j) { |
6325 | | - var ring = polygon[j], k = -1, p; |
6326 | | - if ((p = ring.length - 1) > 0) { |
6327 | | - buffer.push("M"); |
6328 | | - while (++k < p) buffer.push(ring[k], "L"); |
6329 | | - buffer[buffer.length - 1] = "Z"; |
6330 | | - } |
| 6309 | + var buffer = []; |
| 6310 | + pathObject(object, { |
| 6311 | + point: function(x, y) { |
| 6312 | + buffer.push("M", x, ",", y, pointCircle); |
| 6313 | + }, |
| 6314 | + moveTo: function(x, y) { |
| 6315 | + buffer.push("M", x, ",", y); |
| 6316 | + }, |
| 6317 | + lineTo: function(x, y) { |
| 6318 | + buffer.push("L", x, ",", y); |
| 6319 | + }, |
| 6320 | + closePath: function() { |
| 6321 | + buffer.push("Z"); |
6331 | 6322 | } |
6332 | | - } |
| 6323 | + }); |
| 6324 | + return buffer.length ? buffer.join("") : null; |
6333 | 6325 | } |
6334 | | - function polygonArea(coordinates) { |
6335 | | - var sum = area(coordinates[0]), i = 0, n = coordinates.length; |
6336 | | - while (++i < n) sum -= area(coordinates[i]); |
6337 | | - return sum; |
| 6326 | + function pathObject(object, context) { |
| 6327 | + var pathType = pathObjectByType.get(object.type); |
| 6328 | + if (pathType) pathType(object, context); |
6338 | 6329 | } |
6339 | | - function polygonCentroid(coordinates) { |
6340 | | - var polygon = d3.geom.polygon(coordinates[0].map(projection)), area = polygon.area(), centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1), x = centroid[0], y = centroid[1], z = area, i = 0, n = coordinates.length; |
6341 | | - while (++i < n) { |
6342 | | - polygon = d3.geom.polygon(coordinates[i].map(projection)); |
6343 | | - area = polygon.area(); |
6344 | | - centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1); |
6345 | | - x -= centroid[0]; |
6346 | | - y -= centroid[1]; |
6347 | | - z -= area; |
6348 | | - } |
6349 | | - return [ x, y, 6 * z ]; |
| 6330 | + function pathGeometry(geometry, context) { |
| 6331 | + var pathType = pathGeometryByType.get(geometry.type); |
| 6332 | + if (pathType) pathType(geometry, context); |
6350 | 6333 | } |
6351 | | - function area(coordinates) { |
6352 | | - return Math.abs(d3.geom.polygon(coordinates.map(projection)).area()); |
| 6334 | + function pathFeature(feature, context) { |
| 6335 | + pathGeometry(feature.geometry, context); |
6353 | 6336 | } |
6354 | | - var pointRadius = 4.5, pointCircle = d3_geo_path_circle(pointRadius), projection = d3.geo.albersUsa(), projectLine = d3_geo_path_projectLine(projection), projectPolygon = d3_geo_path_projectPolygon(projection), buffer = []; |
6355 | | - var pathType = d3_geo_type({ |
6356 | | - FeatureCollection: function(o) { |
6357 | | - var features = o.features, i = -1, n = features.length; |
6358 | | - while (++i < n) buffer.push(pathType(features[i].geometry)); |
6359 | | - }, |
6360 | | - Feature: function(o) { |
6361 | | - pathType(o.geometry); |
6362 | | - }, |
6363 | | - Point: function(o) { |
6364 | | - buffer.push("M", projection(o.coordinates), pointCircle); |
6365 | | - }, |
6366 | | - MultiPoint: function(o) { |
6367 | | - var coordinates = o.coordinates, i = -1, n = coordinates.length; |
6368 | | - while (++i < n) buffer.push("M", projection(coordinates[i]), pointCircle); |
6369 | | - }, |
6370 | | - LineString: function(o) { |
6371 | | - multiLineString(projectLine(o.coordinates)); |
6372 | | - }, |
6373 | | - MultiLineString: function(o) { |
6374 | | - var lineStrings = o.coordinates; |
6375 | | - for (var i = 0, n = lineStrings.length; i < n; ++i) { |
6376 | | - multiLineString(projectLine(lineStrings[i])); |
6377 | | - } |
6378 | | - }, |
6379 | | - Polygon: function(o) { |
6380 | | - multiPolygon(projectPolygon(o.coordinates)); |
6381 | | - }, |
6382 | | - MultiPolygon: function(o) { |
6383 | | - var polygons = o.coordinates; |
6384 | | - for (var i = 0, n = polygons.length; i < n; ++i) { |
6385 | | - multiPolygon(projectPolygon(polygons[i])); |
6386 | | - } |
6387 | | - }, |
6388 | | - GeometryCollection: function(o) { |
6389 | | - var geometries = o.geometries, i = -1, n = geometries.length; |
6390 | | - while (++i < n) buffer.push(pathType(geometries[i])); |
6391 | | - } |
| 6337 | + function pathFeatureCollection(collection, context) { |
| 6338 | + var features = collection.features, i = -1, n = features.length; |
| 6339 | + while (++i < n) pathFeature(features[i], context); |
| 6340 | + } |
| 6341 | + function pathGeometryCollection(collection, context) { |
| 6342 | + var geometries = collection.geometries, i = -1, n = geometries.length; |
| 6343 | + while (++i < n) pathGeometry(geometries[i], context); |
| 6344 | + } |
| 6345 | + function pathLineString(lineString, context) { |
| 6346 | + projection.line(lineString.coordinates, context); |
| 6347 | + } |
| 6348 | + function pathMultiLineString(multiLineString, context) { |
| 6349 | + var coordinates = multiLineString.coordinates, i = -1, n = coordinates.length; |
| 6350 | + while (++i < n) projection.line(coordinates[i], context); |
| 6351 | + } |
| 6352 | + function pathMultiPoint(multiPoint, context) { |
| 6353 | + var coordinates = multiPoint.coordinates, i = -1, n = coordinates.length; |
| 6354 | + while (++i < n) projection.point(coordinates[i], context); |
| 6355 | + } |
| 6356 | + function pathMultiPolygon(multiPolygon, context) { |
| 6357 | + var coordinates = multiPolygon.coordinates, i = -1, n = coordinates.length; |
| 6358 | + while (++i < n) { |
| 6359 | + var subcoordinates = coordinates[i], j = -1, m = subcoordinates.length; |
| 6360 | + while (++j < m) projection.ring(subcoordinates[j], context); |
| 6361 | + } |
| 6362 | + } |
| 6363 | + function pathPoint(point, context) { |
| 6364 | + projection.point(point.coordinates, context); |
| 6365 | + } |
| 6366 | + function pathPolygon(polygon, context) { |
| 6367 | + var coordinates = polygon.coordinates, i = -1, n = coordinates.length; |
| 6368 | + while (++i < n) projection.ring(coordinates[i], context); |
| 6369 | + } |
| 6370 | + var pointRadius = 4.5, pointCircle = d3_geo_path_circle(pointRadius), projection = d3.geo.albersUsa(); |
| 6371 | + var pathObjectByType = d3.map({ |
| 6372 | + Feature: pathFeature, |
| 6373 | + FeatureCollection: pathFeatureCollection, |
| 6374 | + GeometryCollection: pathGeometryCollection, |
| 6375 | + LineString: pathLineString, |
| 6376 | + MultiLineString: pathMultiLineString, |
| 6377 | + MultiPoint: pathMultiPoint, |
| 6378 | + MultiPolygon: pathMultiPolygon, |
| 6379 | + Point: pathPoint, |
| 6380 | + Polygon: pathPolygon |
6392 | 6381 | }); |
6393 | | - var areaType = path.area = d3_geo_type({ |
6394 | | - FeatureCollection: function(o) { |
6395 | | - var area = 0, features = o.features, i = -1, n = features.length; |
6396 | | - while (++i < n) area += areaType(features[i]); |
6397 | | - return area; |
6398 | | - }, |
6399 | | - Feature: function(o) { |
6400 | | - return areaType(o.geometry); |
6401 | | - }, |
6402 | | - Polygon: function(o) { |
6403 | | - return polygonArea(o.coordinates); |
6404 | | - }, |
6405 | | - MultiPolygon: function(o) { |
6406 | | - var sum = 0, coordinates = o.coordinates, i = -1, n = coordinates.length; |
6407 | | - while (++i < n) sum += polygonArea(coordinates[i]); |
6408 | | - return sum; |
6409 | | - }, |
6410 | | - GeometryCollection: function(o) { |
6411 | | - var sum = 0, geometries = o.geometries, i = -1, n = geometries.length; |
6412 | | - while (++i < n) sum += areaType(geometries[i]); |
6413 | | - return sum; |
6414 | | - } |
6415 | | - }, 0); |
6416 | | - var centroidType = path.centroid = d3_geo_type({ |
6417 | | - Feature: function(o) { |
6418 | | - return centroidType(o.geometry); |
6419 | | - }, |
6420 | | - Polygon: function(o) { |
6421 | | - var centroid = polygonCentroid(o.coordinates); |
6422 | | - return [ centroid[0] / centroid[2], centroid[1] / centroid[2] ]; |
6423 | | - }, |
6424 | | - MultiPolygon: function(o) { |
6425 | | - var area = 0, coordinates = o.coordinates, centroid, x = 0, y = 0, z = 0, i = -1, n = coordinates.length; |
6426 | | - while (++i < n) { |
6427 | | - centroid = polygonCentroid(coordinates[i]); |
6428 | | - x += centroid[0]; |
6429 | | - y += centroid[1]; |
6430 | | - z += centroid[2]; |
6431 | | - } |
6432 | | - return [ x / z, y / z ]; |
6433 | | - } |
| 6382 | + var pathGeometryByType = d3.map({ |
| 6383 | + GeometryCollection: pathGeometryCollection, |
| 6384 | + LineString: pathLineString, |
| 6385 | + MultiLineString: pathMultiLineString, |
| 6386 | + MultiPoint: pathMultiPoint, |
| 6387 | + MultiPolygon: pathMultiPolygon, |
| 6388 | + Point: pathPoint, |
| 6389 | + Polygon: pathPolygon |
6434 | 6390 | }); |
6435 | 6391 | path.projection = function(_) { |
6436 | 6392 | if (!arguments.length) return projection; |
6437 | | - projectLine = d3_geo_path_projectLine(projection = _); |
6438 | | - projectPolygon = d3_geo_path_projectPolygon(projection); |
| 6393 | + projection = _; |
6439 | 6394 | return path; |
6440 | 6395 | }; |
6441 | 6396 | path.pointRadius = function(x) { |
|
0 commit comments