From 924187a74d3acecab6f49b0c3a3256e4a2e89c81 Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 13 Nov 2016 19:56:55 -0800 Subject: [PATCH] Fill and stroke for geo2svg! --- README.md | 28 +++++++++++++++++++++++++++- bin/geo2svg | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9a2957a..973fdaf 100644 --- a/README.md +++ b/README.md @@ -858,7 +858,25 @@ See also [geoproject --precision](#geoproject_precision) and [geo2svg --precisio # geo2svg [options…] [file] [<>](https://github.com/d3/d3-geo-projection/blob/master/bin/geo2svg "Source") -Converts the specified GeoJSON *file* to SVG. With [--newline-delimited](#geo2svg_newline_delimited), each input feature is rendered as a separate [path element](https://www.w3.org/TR/SVG/paths.html); otherwise, a single path element is generated. If the associated [GeoJSON feature](http://geojson.org/geojson-spec.html#feature-objects) has an id, the path element will have a corresponding id attribute. The SVG’s fill is set to none and the stroke is set to black. +Converts the specified GeoJSON *file* to SVG. With [--newline-delimited](#geo2svg_newline_delimited), each input feature is rendered as a separate [path element](https://www.w3.org/TR/SVG/paths.html); otherwise, a single path element is generated. + +By default, the SVG’s [fill](#geo2svg_fill) is set to none and the [stroke](#geo2svg_stroke) is set to black. To override these values on a per-feature basis, the following [GeoJSON feature](http://geojson.org/geojson-spec.html#feature-objects) properties will be propagated to attributes: + +* fill +* fill-rule or fillRule +* fill-opacity or fillOpacity +* stroke +* stroke-width or strokeWidth +* stroke-linecap or strokeLinecap +* stroke-linejoin or strokeLinejoin +* stroke-miterlimit or strokeMiterlimit +* stroke-dasharray or strokeDasharray +* stroke-dashoffset or strokeDashoffset +* stroke-opacity or strokeOpacity + +If the feature has an id, the path element will have a corresponding id attribute. If the feature has a *title* property, the path element will have a title element with the corresponding value. + +Note: per-feature attributes are most useful in conjunction with [newline-delimited](#geo2svg_newline_delimited) input, as otherwise the generated SVG only has a single path element. To set these properties dynamically, pass the input through [ndjson-map](https://github.com/mbostock/ndjson-map/blob/master/README#map). # geo2svg -h
# geo2svg --help @@ -890,6 +908,14 @@ Specify the output height. Defaults to 500. Reduce the precision for smaller output files. Defaults to six digits after the decimal point. See also [d3.geoQuantize](#geoQuantize). +# geo2svg --fill value + +Specify the default output fill color. Defaults to none. + +# geo2svg --stroke value + +Specify the default output stroke color. Defaults to black. + # geo2svg -n
# geo2svg --newline-delimited diff --git a/bin/geo2svg b/bin/geo2svg index fd40aec..9d0df54 100755 --- a/bin/geo2svg +++ b/bin/geo2svg @@ -15,6 +15,8 @@ commander .option("-h, --height ", "output height", 500) .option("-p, --precision ", "number of output digits after the decimal point", 6) .option("-n, --newline-delimited", "accept newline-delimited JSON") + .option("--fill ", "default fill", "none") + .option("--stroke ", "default stroke", "black") .parse(process.argv); if (commander.args.length === 0) commander.args[0] = "-"; @@ -41,14 +43,30 @@ writer.write("" + os.EOL + " width=\"" + +commander.width + "\"" + " height=\"" + +commander.height + "\"" + " viewBox=\"0 0 " + +commander.width + " " + +commander.height + "\"" - + " fill=\"none\" stroke=\"black\"" + + (commander.fill != "black" ? " fill=\"" + attr(commander.fill) + "\"" : "") + + (commander.stroke != "none" ? " stroke=\"" + attr(commander.stroke) + "\"" : "") + ">" + os.EOL); function transform(d) { + var p = d.properties, v; return writer.write(" " + os.EOL); + + ">" + + ((v = p && p["title"]) != null ? "" + text(v + "") + "" : "") + + "" + os.EOL); } function end() { @@ -59,6 +77,16 @@ function abort(error) { console.error(error.stack); } -function quote(string) { +function text(string) { + return string.replace(/[&<>]/g, function(character) { + switch (character) { + case "&": return "&"; + case "<": return "<"; + case ">": return ">"; + } + }); +} + +function attr(string) { return string.replace(/"/g, """); }