Overview of GeoJSON (Geo+JSON)¶
To begin with, JavaScript Object Notation (JSON) is a lightweight, text-based, language-independent data interchange format.
an old style example the JSON Schema for a geospatial object
{
"$id": "https://example.com/geographical-location.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "AGIC Venue",
"description": "A geographical coordinate where this workshop is being taught",
"required": [ "latitude", "longitude" ],
"type": "object",
"properties": {
"latitude": {
"type": "number",
"minimum": -90,
"maximum": 90
},
"longitude": {
"type": "number",
"minimum": -180,
"maximum": 180
}
}
}
{
"latitude": 34.5510,
"longitude": -112.4471
}
GeoJSON is a format for encoding a variety of geographic data structures.
A GeoJSON Object may represent a region of space: a Geometry, a Feature, or a list of Features called a FeatureCollection.
Geometry¶
There are seven types of geometric shapes which can be defined in GeoJSON. Where the "type" can be any of the following:
Type | Geometry |
---|---|
Point | |
LineString | |
Polygon | |
MultiPoint | |
MultiLineString | |
MultiPolygon | |
GeometryCollection |
-
Point, LineString and Polygon are single type geometry objects
-
MultiPoint, MultiLineString, and MultiPolygon are multipart geometry objects
Each of the examples below represents a valid and complete GeoJSON object:
Point¶
GeoJSON Point Schema
{
"type": "Point",
"coordinates": [-112.4471, 34.5510]
}
A Point
geometry is represented by a single set of longitude then latitude coordinates, optionally elevation (above mean sea level) can also be included for a third axis.
LineString¶
GeoJSON LineString Schema
{
"type": "LineString",
"coordinates": [
[-112.4470, 34.5510], [-112.4695, 34.541]
]
}
A Line
geometry uses two or more sets of coordinate pairs to create a line or multi-line.
Polygon¶
GeoJSON Polygon Schema
{
"type": "Polygon",
"coordinates": [
[[-112.485, 34.529], [-112.445, 34.529], [-112.445, 34.559], [-112.485, 34.559], [-112.485, 34.529]]
]
}
A Polygon
has at least three sets of coordinate pairs which close the shape.
MultiPoint¶
GeoJSON MultiPoint Schema
{
"type": "MultiPoint",
"coordinates": [
[-112.4470, 34.5510],
[-112.4695, 34.541]
]
}
A MultiPoint
geometry is represented by multiple coordinate point pairs
MultiLineString¶
GeoJSON MultiLineString Schema
{
"type": "MultiLineString",
"coordinates": [
[[-112.44708, 34.5510], [-112.46953, 34.540924]],
[[-112.4471, 34.5510], [-112.4541,34.54447], [-112.46953, 34.540924]]
]
}
A MultiLineString
uses multiple lines which are not connected.
MultiPolygon¶
GeoJSON MultiPolygon Schema
{
"type": "MultiPolygon",
"coordinates": [
[
[[-112.0, 35.0], [-112.0, 34.0], [-113.0, 34.0], [-113.0, 35.0], [-112.0, 35.0]]
],
[
[[-112.50, 35.50], [-112.50, 34.50], [-113.50, 34.50], [-113.50, 35.50], [-112.50, 35.50]]
],
[
[[-111.50, 34.50], [-111.50, 33.50], [-112.50, 33.50], [-112.50, 34.50], [-111.50, 34.50]]
]
]
}
A MultiPolygon
uses multiple lines to provide more than one set of polygons.
GeometryCollection¶
GeoJSON GeometryCollection
{
"type": "GeometryCollection",
"geometries": [{
"type": "Point",
"coordinates": [-112.4471, 34.5510]
}, {
"type": "LineString",
"coordinates": [
[-112.4470, 34.5510], [-112.4695, 34.541]
]
}]
}
The GeometryCollection
introduces a new set of braces {}
which separate different geometries.
Feature¶
GeoJSON Feature
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-112.4470, 34.5510]
},
"properties": {
"name": "AGIC Venue"
}
}
A Feature
provides a new set of properties:
which allow metadata and annotation of the geometry.
FeatureCollection¶
GeoJSON FeatureCollection Point
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"Location Name" : "Prescott Resort and Conference Center"
},
"geometry": {
"type": "Point",
"coordinates": [
-112.44677424430846,
34.55109119815299
]
}
}
]
}
A full FeatureCollection
provides us with the ability to create multiple Features
GeoJSON FeatureCollection with multiple attributes
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
-112.44717121124268,
34.551069106918945
],
[
-112.45414495468138,
34.54447682068866
],
[
-112.46953010559082,
34.540924192549795
]
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-112.44691371917723,
34.551210490715455
]
}
}
]
}
Related JSON types¶
TopoJSON¶
TopoJSON is an extension of GeoJSON that encodes topology and is more efficient than GeoJSON.
TopoJSON use "arcs" as a way of connecting coordinate pairs, reducing the size of the file, removing duplicate coordinates, and improving performance in the browser.
TopoJSON Schema
{
"type": "Topology",
"objects":
{
"collection":
{
"type": "Polygon",
"arcs":[[0]]
}
},
"arcs": [[[0,0],[9999,0],[0,9999],[-9999,0],[0,-9999]]],
"transform":
{
"scale":[0.000004000400040004626,0.0000030003000300024037],
"translate": [-112.485,34.529]
},
"bbox":[-112.485,34.529,-112.445,34.559]
}
Newline-delimited GeoJSON¶
Newline Delimited GeoJSON is particularly convenient for transformation and processing. Each line can be read independently, parsed, and processed, without the entire file ever having to be parsed in memory, which is great for huge files.
NLD GeoJSON remove "FeatureCollection"
NLD GeoJSON Schema
{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[-112.44691371917723,34.551210490715455]}}
{"type":"Feature","properties":{},"geometry":{"type":"LineString","coordinates":[[-112.4470,34.5510],[-112.4695,34.541]]}}
{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-112.485,34.529],[-112.445,34.529],[-112.445,34.559],[-112.485,34.559],[-112.485,34.529]]]}}
GeoJSON Webpages¶
geojson.io - create, view, edit GeoJSON in your browser
shp2json cross-platform streaming parser for the ESRI Shapefile spatial data format.
Is GeoJSON really 'cloud native'?
Not really. NLD GeoJSON is closer to being cloud-native format than GeoJSON, but both have their short-comings.
The ubiquity of JSON on web and its recommended use in cloud-native formats is why we're teaching it. You'll learn in the later sections GeoJSON is the most common vector for querying cloud optimized data formats.
Chris Holmes recent Blog Post on 'Cloud Native Vectors'
Chris Holmes older blog post on 'Towards a Cloud-Native Geospatial standards baseline'
Hands-On¶
Step 1 Go to GeoJSON.io¶
Play around creating different GeoJSON Geometries and Feature Collections
- Zoom into an area of interest (suggest selecting the geographic area around Prescott, AZ)
- Practice creating points, lines, polygon, and a multi-polygons.
- Add additional metadata to the "properties"
In the "properties" : {}
field, add additional metadata:
"type": "Feature",
"properties": {
"city name" : "Prescott",
"state" : "Arizona"
},
JSON formatting
JSON is very specific when it comes to line spacing, braces {}
, quotations ""
for strings, and commas ,
at the end of a line inside a feature.
-
Try to use a text editor with JSON formatting turned on, this will help you to debug hand-written or edited JSON and GeoJSON files that have errors.
-
Spaces between the colons
:
are optional but may help improve readability. -
When you add more than one new line, you must use a comma at the end.
Step 2 Export a GeoJSON¶
-
Click on "Save" on the GeoJSON.io website
-
Select the
GeoJSON
Format option. -
Download to your local computer in an easy to relocate folder
Step 3 Option 1: Open the GeoJSON in QGIS¶
-
Open QGIS
-
In the "Layers" then "Add Layer" and then "Add Vector Layer"
-
Choose the "Source Type and select "File" for a file on your computer
-
Navigate to your downloads folder and select a downloaded
.geojson
in the "Source" "Vector Dataset(s)" field.
Loading GeoJSON from HTTP(S)¶
GeoJSON can be read directly from the web, without downloading them.
Here we're going to use the USGS 3DEP lidar boundaries layer which is hosted on GitHub:
https://github.com/hobuinc/usgs-lidar/raw/master/boundaries/resources.geojson
Using GitHub to view GeoJSON
Note how resources.geojson
is visible as a leaflet map directly in GitHub.
GeJSON can also be visualized in VS Code and Jupyter Lab the same way using map extensions for Folium and Leaflet.
Troubleshooting import HTTPS GeoJSON layers in QGIS
Change The Environmental Setting Variables In QGIS
-
Go to Setting Menu >> Options
In the Options window:
Go to System
-
Expand Environment:
Check the box to Use custom vairables
-
Then add the following variables
variable 1:
GDAL_HTTP_USERAGENT
variable value 1:
Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
variable 2:
GDAL_HTTP_UNSAFESSL
variable value 2:
YES
-
Restart QGIS and them try to add the GeoJSON file link again.
Step 3 Option 2: Open the GeoJSON in ArcGIS Online¶
Log into ArcGIS Online and select the MapViewer
https://ua-gisug.maps.arcgis.com/apps/mapviewer/index.html
Open the GeoJSON in an IDE (like VS Code or Jupyter)¶
VS Code can be used to view and edit JSON and GeoJSON
Step 4 (Optional): Convert a SHP file to GeoJSON¶
QGIS can export .shp
as .geojson
directly.