0

I have several GeoJSON files which I would visualize. I imagine something like

geojson2png input.geojson output.png

and some parameters for the resolution / bounding box I want to visualize.

http://geojson.io is awesome, but it is not feasable to upload it and take a screenshot for many files. I've seen that it is open source and tried geojsonio.py, but that uploads the data to a public gist (and has problems).

If I just want the style to be applied as it is on geojson.io, with no underlying map, can I create it locally?

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958

1 Answers1

0
from functools import partial
import json

# 3rd party modules
from descartes import PolygonPatch
from shapely import ops
from shapely.geometry.geo import shape
import matplotlib.pyplot as plt
import pyproj


def project(polygon):
    geom_area = ops.transform(
        partial(
            pyproj.transform,
            pyproj.Proj(init='EPSG:4326'),
            pyproj.Proj(
                proj='aea',
                lat1=polygon.bounds[1],
                lat2=polygon.bounds[3])),
        polygon)
    return geom_area


def visualize_geojson(geojson_filepath, png_destination='out.png'):
    with open(geojson_filepath) as f:
        features = json.loads(f.read())

    fig = plt.figure(figsize=(25, 25), dpi=300)
    ax = fig.gca()

    for feature in features['features']:
        fc = feature['properties'].get('fill', '#555555')
        polygon = shape(feature['geometry'])
        ax.add_patch(PolygonPatch(project(polygon),
                                  fc=fc,
                                  ec='#555555',
                                  alpha=0.5,
                                  zorder=2))
    ax.axis('scaled')
    plt.savefig(png_destination, dpi=300, pad_inches=0)
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958