From cd4014d4e0dd33390fdefb94625b1def8f50dadc Mon Sep 17 00:00:00 2001 From: Jim Cottrell Date: Tue, 15 Oct 2024 14:56:45 -0600 Subject: [PATCH] Fix `coordinateConverter` typos and add to types (#36) --- README.md | 2 +- dist/geojson2svg.js | 2 +- dist/geojson2svg.js.map | 2 +- dist/geojson2svg.min.js | 2 +- src/index.d.ts | 3 +++ src/index.js | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f600842..947fcf5 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ Here are all options available for initializing the instance. * **mapExtentFromGeojson:** boolean, if true `mapExtent` is calculated from GeoJSON data that is passed in `.convert` function. * **fitTo:** 'width' | 'height' Fit output SVG map to width or height. If nothing is provided, the program tries to fit the data within width or height so that full mapExtent is visible in viewport. -* **coordinateCoverter:** 'function' to convert input GeoJSON coordinates while converting to SVG. This function should take coordinates of a point `[x,y]` and returns transformed point `[x, y]`. +* **coordinateConverter:** 'function' to convert input GeoJSON coordinates while converting to SVG. This function should take coordinates of a point `[x,y]` and returns transformed point `[x, y]`. * **pointAsCircle:** true | false, default is false. For point GeoJSON return circle element for option: ``` { "pointAsCircel": true } ``` output SVG string would be: diff --git a/dist/geojson2svg.js b/dist/geojson2svg.js index e00464f..b6573a0 100644 --- a/dist/geojson2svg.js +++ b/dist/geojson2svg.js @@ -338,7 +338,7 @@ var GeoJSON2SVG = function(options = {}) { this.options = options; this.viewportSize = options.viewportSize || {width: 256, height: 256}; - if (options.coordinateCoverter + if (options.coordinateConverter && typeof options.coordinateConverter != 'function') { throw new Error('"coordinateConverter" option should be function'); diff --git a/dist/geojson2svg.js.map b/dist/geojson2svg.js.map index a53819a..183cab2 100644 --- a/dist/geojson2svg.js.map +++ b/dist/geojson2svg.js.map @@ -20,6 +20,6 @@ "module.exports = function(gj) {\n var coords, bbox;\n if (!gj.hasOwnProperty('type')) return;\n coords = getCoordinatesDump(gj);\n bbox = [ Number.POSITIVE_INFINITY,Number.POSITIVE_INFINITY,\n Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY,];\n return coords.reduce(function(prev,coord) {\n return [\n Math.min(coord[0], prev[0]),\n Math.min(coord[1], prev[1]),\n Math.max(coord[0], prev[2]),\n Math.max(coord[1], prev[3])\n ];\n }, bbox);\n};\n\nfunction getCoordinatesDump(gj) {\n var coords;\n if (gj.type == 'Point') {\n coords = [gj.coordinates];\n } else if (gj.type == 'LineString' || gj.type == 'MultiPoint') {\n coords = gj.coordinates;\n } else if (gj.type == 'Polygon' || gj.type == 'MultiLineString') {\n coords = gj.coordinates.reduce(function(dump,part) {\n return dump.concat(part);\n }, []);\n } else if (gj.type == 'MultiPolygon') {\n coords = gj.coordinates.reduce(function(dump,poly) {\n return dump.concat(poly.reduce(function(points,part) {\n return points.concat(part);\n },[]));\n },[]);\n } else if (gj.type == 'Feature') {\n coords = getCoordinatesDump(gj.geometry);\n } else if (gj.type == 'GeometryCollection') {\n coords = gj.geometries.reduce(function(dump,g) {\n return dump.concat(getCoordinatesDump(g));\n },[]);\n } else if (gj.type == 'FeatureCollection') {\n coords = gj.features.reduce(function(dump,f) {\n return dump.concat(getCoordinatesDump(f));\n },[]);\n }\n return coords;\n}\n", "//index.js \n(function() { \n\tvar singles = ['Point', 'LineString', 'Polygon'];\n\tvar multies = ['MultiPoint', 'MultiLineString', 'MultiPolygon'];\n\tfunction explode(g) {\n\t if( multies.indexOf(g.type) > -1) {\n\t return g.coordinates.map(function(part) {\n\t var single = {};\n\t single.type = g.type.replace('Multi','');\n\t single.coordinates = part;\n if(g.crs) single.crs = g.crs;\n\t return single;\n\t }); \n\t } else {\n\t return false;\n\t }\n\t}\n\tfunction implode(gs) {\n\t var sameType = gs.every(function(g) { \n\t return singles.indexOf(g.type) > -1;\n\t })\n var crs = gs[0].crs || 0;\n var sameCrs = gs.every(function(g) {\n var gcrs = g.crs || 0;\n return gcrs == crs;\n });\n\t if(sameType && sameCrs) {\n\t var multi = {};\n\t multi.type = 'Multi' + gs[0].type;\n\t multi.coordinates = [];\n if(crs != 0) multi.crs = crs;\n\t gs.forEach(function(g) {\n\t multi.coordinates.push(g.coordinates);\n\t });\n\t return multi;\n\t } else {\n\t return false;\n\t }\n\t};\n\tvar multigeojson = {\n\t explode: explode,\n\t implode: implode\n\t};\n\tif(typeof module !== 'undefined' && module.exports) {\n\t module.exports = multigeojson;\n\t} else if(window) {\n\t window.multigeojson = multigeojson;\n\t}\n})();\n", "//converter.js\nvar multi = require('multigeojson');\n\nfunction getCoordString(coords,res,origin, precision, opt) {\n //origin - svg image origin\n var convertedCoords = coords.map(function(coord) {\n if (opt.coordinateConverter) {\n coord = opt.coordinateConverter(coord);\n }\n return [(coord[0] - origin.x)/res, (origin.y - coord[1])/res];\n });\n var coordStr = convertedCoords.map(function (coord) {\n if (precision) {\n return coord[0].toFixed(precision) + ',' + coord[1].toFixed(precision);\n } else {\n return coord[0] + ',' + coord[1];\n }\n });\n return coordStr.join(' ');\n}\nfunction addAttributes(ele,attributes) {\n var part = ele.split('/>')[0];\n for(var key in attributes) {\n if(attributes[key]) {\n part += ' ' + key + '=\"' + attributes[key] + '\"';\n }\n }\n return part + ' />';\n}\n\nfunction point(geom,res,origin,opt) {\n var r = opt && opt.r ? opt.r : 1;\n var pointAsCircle = opt && opt.hasOwnProperty('pointAsCircle')\n ? opt.pointAsCircle : false;\n var coords = getCoordString([geom.coordinates],res,origin,opt.precision, opt);\n if (pointAsCircle) {\n return [coords];\n } else {\n return [\n 'M' + coords\n + ' m'+ -r+ ',0'+ ' a'+r+','+ r + ' 0 1,1 '+ 2*r + ','+0\n + ' a'+r+','+ r + ' 0 1,1 '+ -2*r + ','+0\n ];\n }\n}\nfunction multiPoint(geom,res,origin,opt) {\n var explode = opt && opt.hasOwnProperty('explode') ? opt.explode : false;\n var paths = multi.explode(geom).map(function(single) {\n return point(single,res,origin,opt)[0];\n });\n if(!explode) return [paths.join(' ')];\n return paths;\n\n}\nfunction lineString(geom,res,origin,opt) {\n var coords = getCoordString(geom.coordinates,res,origin,opt.precision, opt);\n var path = 'M'+ coords;\n return [path];\n}\nfunction multiLineString(geom,res,origin,opt) {\n var explode = opt && opt.hasOwnProperty('explode') ? opt.explode : false;\n var paths = multi.explode(geom).map(function(single) {\n return lineString(single,res,origin,opt)[0];\n });\n if(!explode) return [paths.join(' ')];\n return paths;\n}\nfunction polygon(geom,res,origin,opt) {\n var mainStr,holes,holeStr;\n mainStr = getCoordString(geom.coordinates[0],res,origin,opt.precision, opt);\n if (geom.coordinates.length > 1) {\n holes = geom.coordinates.slice(1,geom.coordinates.length);\n }\n var path = 'M'+ mainStr;\n if(holes) {\n for(var i=0;i [number, number], + /** a number, precision of output svg coordinates. */ precision?: number diff --git a/src/index.js b/src/index.js index 4b25b5c..272473f 100644 --- a/src/index.js +++ b/src/index.js @@ -16,7 +16,7 @@ var GeoJSON2SVG = function(options = {}) { this.options = options; this.viewportSize = options.viewportSize || {width: 256, height: 256}; - if (options.coordinateCoverter + if (options.coordinateConverter && typeof options.coordinateConverter != 'function') { throw new Error('"coordinateConverter" option should be function');