Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phrogz/svg2geojson
Converts an SVG file with added geo-referencing tags into one or more GeoJSON files.
https://github.com/phrogz/svg2geojson
Last synced: 12 days ago
JSON representation
Converts an SVG file with added geo-referencing tags into one or more GeoJSON files.
- Host: GitHub
- URL: https://github.com/phrogz/svg2geojson
- Owner: Phrogz
- License: mit
- Created: 2017-06-16T13:54:56.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-04-10T19:15:58.000Z (over 1 year ago)
- Last Synced: 2024-10-26T12:34:51.612Z (13 days ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 59
- Watchers: 4
- Forks: 30
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SVG 2 GeoJSON
Converts an SVG file with added geo-referencing tags into one or more GeoJSON files.
## Installing
`npm install svg2geojson`
## Geo-Referencing Tags
You must place two `GeoItems` inside a [Prognoz MetaInfo](http://help.prognoz.com/8.0/en/mergedProjects/Specifications/svgmapspecification/structure/svgmap_structure.htm) element as a direct child of the `` element at the root of your document.
~~~xml
~~~
These map opposing X/Y corners in your SVG coordinate space to Longitude/Latitude coordinates on the world. _Note that the SVG coordinate space has Y increasing down (toward the south), while Latitude increases upwards (towards the north)._
## Usage
**Running the binary from the command line**:
~~~
$ npm install -g svg2geojson
$ svg2geojson file.svg # Writes file.geojson
$ svg2geojson file.svg --layers # Writes file.geojson, file-layer1Name.geojson, …
# See svg2geojson --help for more parameters
~~~**Running as a Node.js library**:
~~~ js
const { geoFromSVGFile, geoFromSVGXML } = require('svg2geojson.js');// …reading from file on disk
geoFromSVGFile( 'my.svg', layers => {
layers.forEach( layer => {
let json = JSON.stringify(layer.geo); // Turn JS object into JSON string
console.log(`Layer Named: "${layer.name}"`);
console.log(json);
});
}, {layers:true, tolerance:0.5} );// …processing SVG code as a string
const svg = ``;
geoFromSVGXML( svg, layer => {
let json = JSON.stringify(layer.geo); // Turn JS object into JSON string
console.log(json);
} );
~~~See the output of `svg2geojson --help` for the options you can pass to the functions, and their default values.
## Preparing Paths
SVG allows `` elements with an arbitrary number of overlapping subpaths, with some of them being 'positive' space and some 'negative' space. In SVG these subpaths may be oriented clockwise or counter-clockwise, and added in any order.
GeoJSON only allows a `Polygon` to have a single 'positive' subpath (and an arbitrary number of additional 'hole' subpaths). To make it easier for the code to detect which subpath is the 'positive' subpath you must currently:
1. Have only one positive subpath per ``.
2. Ensure that the positive subpath is the first subpath in a ``.## TODO (AKA Known Limitations)
* Support modes of projection unmapping
* Support non-rectangular, inverse bilinear unmappings
* Add more command-line options to control JSON formatting.
* Treat `` as `MultiPolygon`, `GeometryCollection`, or `MultiLineString` as appropriate. Currently items within a group are flattened as individual `Feature` items in the GeoJSON.
* Treat `` with multiple positive subpaths as a `MultiPolygon`. (This requires figuring out which holes apply to which positive subpaths.)