Plot GeoJSON Function

Use this function to render a GeoJSON in SVG and HTML output.

We'll cover the following

Plot GeoJSON

The GeoJSON file is a text file with the geometry feature expressed in text strings. When we create a Shapely object from a GeoJSON, we have the possibility to render the geometry as a Scalable Vector Graphic (SVG). SVG is an XML-based markup language for describing two-dimensional based vector graphics.

One advantage of the SVG representation over GeoJSON is that the HTML standard has a container for SVG graphics (<svg>), making it easier to embed an SVG graphic on a page, for example. In this scenario, to display the GeoJSON geometry, we are going to use a Shapely internal _repr_svg_() function.

In line 7, we first create a Shapely geometry from the given GeoJSON. Then, in line 10, we store the SVG representation in the svg_text string. If we want to output it as HTML (to be rendered in a web browser, for example) we can create a very simple HTML template and embed the corresponding SVG figure (lines 16 to 26).

Get hands-on with 1200+ tech skills courses.