Skip to main content

Draw geodata in SVG

Project description

SVGIS

Create SVG drawings from vector geodata files (SHP, geoJSON, etc).

svgis draw input.shp -o out.svg
svgis draw south_dakota.shp south_dakota.geojson -o dakota.svg

Install

Requires fiona, which in turn requires GDAL.

Before installing, run the following on OS X: brew install gdal. On Linux: sudo apt-get -qq install -y libgdal1-dev.

Command line tools

The svgis command line tool has four commands: draw, project, scale and style. Svgis scale and svgis style will add a scaling factor or CSS style to an existing SVG file. Svgis project is a utility for printing the projection that svgis draw will generate for given bounding boxes.

The main command is svgis draw, which generates SVGs from input geodata.

svgis draw

–bounds

Takes four arguments in min-lon, min-lat, max-lon, max-lat order.

This example draw the portion of the input file between latitudes 40 and 41 and longitudes -74 and -73 (roughly the New York City area).

svgis draw --bounds -74 40 -73 41 in.geojson out.svg

Note that coordinates are given in longitude, latitude order, since in the world of the computer, it’s better to be consistent with things like (x, y) order.

–scale

A integer scale factor. The map will be scaled by the inverse of this number.

While SVG is a vector format, clients may have trouble handling very large numbers. Use the scale option to scale down the output. Dimensions in the map will be divided by this number (so larger numbers yield smaller coordinates in the output SVG).

svgis draw --scale 1000 in.shp -o out.svg

–project

The project argument accept a particular projection or a keyword that helps SVGIS pick a projection for you.

  • EPSG code

  • Proj4 string

  • Either the ‘utm’ or ‘local’ keyword

The the flag isn’t, SVGIS will check to see if the file is already in non lat-lng projection (e.g. a state plane system or the British National Grid). If the first input file is projected, that projection will be used for the output. If the first file is in lat-long coordinates, a local projection will be generated, just like if --projection-method=local was given.

This example will draw an svg with EPSG:2908, the New York Long Island state plane projection:

svgis draw --project EPSG:2908 nyc.shp -o nyc.svg

This example uses a Proj.4 string to draw with the North America Albers Equal Area Conic projection, which doesn’t have an EPSG code.

svgis draw in.shp -o out.svg \
    --project "+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"

With the utm keyword, SVGIS attempts to draw coordinates in the local UTM projection. The centerpoint of the bounding box will be used to pick the zone. Expect poor results for input data that crosses several UTM boundaries.

svgis draw --project utm in.shp -o out.svg

When the local argument is given, SVGIS will generate a Transverse Mercator projection that centers on the input bounding box. This generally gives good results for an region roughly the size of a large urban area.

svgis draw --project local input.shp -o out.svg
svgis draw -j local input.shp -o out.svg

To properly convert the input coordinate, svgis needs to know your input projection. If the input file doesn’t specify an internal projection, SVGIS will assume that the coordinates are given in WGS84.

–style

The style parameter takes either a CSS file or a CSS string.

svgis draw --style style.css in.shp -o out.svg
svgis draw --style "line { stroke: green; }" in.shp -o out.svg

–padding

Adds a padding around the output image. Accepts an integer in svg units.

svgis draw --padding 100 in.shp -o out.svg

–no-viewbox

By default, SVGIS uses a viewbox. If you have a problem opening the created svg file in your drawing program (e.g. Adobe Illustrator), try the ‘–no-viewbox’ option, which will create an svg where the contents are translated into the frame.

svgis draw --no-viewbox in.shp -o out.svg
svgis draw -x in.shp -o out.svg

–class-fields and –id-field

Use these options to specify fields in the source geodata file to use to determine the class or id attributes of the output SVG features. In the output fields, spaces will be replaced with underscores.

For example, the Natural Earth admin_0 file contains nation polygons, and includes continent, income_grp and name fields:

svgis draw --class-fields continent,income_grp --id-field name ne_110m_admin_0_countries.shp -o out.svg

The result will include something like:

<g id="ne_110m_admin_0_countries">
    <g class="ne_110m_admin_0_countries Asia _5_Low_income" id="Afghanistan">/* Afghanistan */</g>
    <g class="ne_110m_admin_0_countries Africa _3_Upper_middle_income" id="Angola">/* Angola */</g>
    /* ... */
    <g class="ne_110m_admin_0_countries Africa _5_Low_income" id="Zimbabwe">/* Zimbabwe */</g>
</g>

The name of a layer (ne_110m_admin_0_countries) will always be in the classes of its child elements. This makes addressing a particular layer easy, given that some implementations of SVG don’t properly handle id elements (e.g. Adobe Illustrator, ImageMagick).

Note that the ‘income_grp’ field contains values like “4. Lower middle income”, SVGIS has sanitized them for use in the output svg.

Each layer is always wrapped in a group (<g>) with id equal to the name of its source layer.

Further examples

Draw the outline of the contiguous United States, projected in Albers:

curl -O http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_nation_20m.zip
unzip cb_2014_us_nation_20m.zip
svgis draw --project EPSG:5070 --scale 1000 --bounds -124 20.5 -64 49 cb_2014_us_nation_20m.shp -o us.svg

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

svgis-0.2.1.tar.gz (264.4 kB view details)

Uploaded Source

Built Distributions

svgis-0.2.1-py3-none-any.whl (255.4 kB view details)

Uploaded Python 3

svgis-0.2.1-py2-none-any.whl (255.4 kB view details)

Uploaded Python 2

File details

Details for the file svgis-0.2.1.tar.gz.

File metadata

  • Download URL: svgis-0.2.1.tar.gz
  • Upload date:
  • Size: 264.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for svgis-0.2.1.tar.gz
Algorithm Hash digest
SHA256 04f283a59dec136076ebe381cd642c961f230727f56c2828c910e8d873e1f305
MD5 1575a79ab303adf3f48661e74ed1e000
BLAKE2b-256 1433e420bfd0cf0f2fe1fbcfe472c8011a41c8de9489a63599c423e6a30c2c45

See more details on using hashes here.

File details

Details for the file svgis-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for svgis-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ff56e9a359e91dae72b1d63bc7f113ec5e947669486f744aff0fba7065b56188
MD5 20a15e9794c60620d9d49a844d0e62f3
BLAKE2b-256 d437ea585de444d8dcdaf690ddb8537fd0e249f9d24105813eca66b784bfd06e

See more details on using hashes here.

File details

Details for the file svgis-0.2.1-py2-none-any.whl.

File metadata

File hashes

Hashes for svgis-0.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 705b25b6f6c659602383c7cde84092a8fa2f5a1e9ac801b5d5f33633823cc0ce
MD5 b8125fea4f355a48887d8724c7fbe42a
BLAKE2b-256 3d346363886fedeee2d0905f18f3a4e57dda0d35f127b39d4243d9c8513a19d9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page