Back to 5.0.0-doc main page
Introduction
GeoJSON is an open standard data format based on JSON (JavaScript Object Notation) that is a very popular data format widely used by many web-apps as a replacement for XML.The intended use of GeoJSON is the extension of the basic capabilities of JSON so to adequately support geographic features including both Geometries and non-spatial attributes.
GeoJSON, which has existed for many years, was initially based on a loose and informal data specification.
Only very recently (2016) it has finally become a respectable standard format based on a formal specification, RFC 7946, released by IETF (Internet Engineering Task Force).
Note: RFC 7946 introduced several relevant requirements and restrictions, so that pre-RFC and post-RFC GeoJSON files are not mutually interoperable.
A very remarkable feature of RFC 7946 is that it's explicitly declared to be a fixed and immutable specification.
There will never be updated versions of GeoJSON; even the slightest change will require a rename from GeoJSON to something else.
Such a restriction is obviously intended to ensure a very strong stability during its lifetime (and represents a strong reaction to the many GML versions that abruptly introduced radical changes upsetting the well established behavior of existing implementations).
The most obvious competitors of GeoJSON are the ESRI Shapefile and GML
The following chart will quickly list the main differences between them.
Topic | Shapefile | GML | GeoJSON | Remarks | |
---|---|---|---|---|---|
File organization | At least three independent files sharing the same name and respectively identified by suffixes .shp, .shx and .dbf
|
GML is based on XML, and is just a single, monolithic text file. As any other XML file, GML too can be strongly constrained to conform to a formally defined XML Schema |
Single monolithic text file. Similar to XML, but intended to be far simpler and less verbose. |
The three-files layout of Shapefile is clearly obsolete, and it frequently poses many headaches causing unexpected troubles. The single-file layout adopted by both GML and GeoJSON is clearly better and safer, and being text files they can be easily inspected and eventually debugged just using any generic text editor without requiring any specific tool. |
|
Geometry classes |
Notes:
|
GML allows many different ways to define the same type of Geometry, and the specifications have radically changed from version to version. GML has a really impressive flexibility (e.g. each single Geometry can freely declare its own SRID), but at the cost of imposing an overwhelming complexity. |
Notes:
|
|
|
Dimensions |
|
|
Note: RFC 7946 just supports 2 or 3 coordinates, and the third value (when declared) is always expected to correspond to an Elevation (Z axis). Supporting XYM or XYZM is not technically unfeasible. Both writers and readers could support such options, but all this is surely outside the standard and will surely impair the universal portability of any non canonical file. |
GeoJSON lacks the capability to support XYM and XYZM, other than by adopting (and to others unknown) vicious tricks. May well be it's not a forbidding limitation in many common cases, but it's indisputably a limitation. |
|
SRID |
Not internally declared by the Shapefile itself. Deploying a further .prj file describing the intended SRID is the usual solution adopted by ESRI itself, but correctly parsing these extra files is an usually flimsy process falling outside real capabilities of many third party readers. |
Each single Geometry is allowed to freely define its own SRID, as well as defining the SRID for the whole layer. |
Accordingly to RFC 7946 all coordinates are always expected to be expressed as longitudes and latitudes ((i.e: x-position and y-position). So any canonical GeoJSON file is always expected to reference SRID=4326 WGS 84 (i.e. the SRID is not stored within the GeoJSON file). Using any other SRID is technically possible, but requires a conventional agreement between writers and readers, but all this is surely outside the standard and will surely impair the universal portability of any non canonical file. |
The unique effective solution is the one adopted by GML. Both Shapefile and GeoJSON are clearly inferior under this peculiar aspect. |
|
Non-spatial attributes |
Note: all attribute names are limited to a length of max. 10 bytes. There is no safe way for declaring NULL values. |
Any possible datatype you can imagine. And defining further derived datatypes is an option supported by the XML Schema. Note: attribute names and text values can have any arbitrary length. |
Note: attribute names can have any arbitrary length. |
|
|
Charset encoding
|
Not internally defined by the Shapefile itself. Attempting to guess the appropriate charset encoding required by some Shapefile is more a magic art than rational science. |
Always internally defined by the GML/XML file itself. |
RFC 7946 strictly requires that all GeoJSON files must be encoded as UTF-8 In pure theory both UTF-16 and UTF-32 could be used for encoding a legitimate GeoJSON file, but such options seems to be very rarely (if ever) adopted in real world. |
|
Skeletal GeoJSON anatomy
{ "type": "FeatureCollection", "features": [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [102.0, 0.5] }, "properties": { "prop0": "value0" } }, { "type": "Feature", "geometry": { "type": "LineString", "coordinates": [ [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] }, "properties": { "prop0": "value0", "prop1": 0.0 } }, { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ] }, "properties": { "prop0": "value0", "prop1": {"this": "that"} } }] }As you can easily notice, GeoJSON has a plain regular structure and is decisively most concise and less verbose than GML/XML:
- Every GeoJSON file must contain a FeatureCollection object.
- A FeatureCollection contains one or more individual Feature objects.
- Each Feature is expected to declare a Geometry and a properties array.
- Each property corresponds to an attribute with a name and a value.
- Each Feature is expected to declare a Geometry and a properties array.
- A FeatureCollection contains one or more individual Feature objects.
Short conclusions
GeoJSON is widely adopted by many web-apps; being a notation directly based on JavaScript it has a natural integration in popular client-side JS libraries such as OpenLayers and Leaflet.It's adequately supported by GDAL, and consequently by many others free/libre sw components (as e.g. MapServer and GeoServer).
Outside the web-gis arena GeoJSON still has limited adoption, and it's a real pity because under many aspects it's a very valid full replacement for the nowadays irremediably obsolete Shapefile.
Starting since version 5.0.0 SpatiaLite offers full supports to GeoJSON as defined by RFC 7946.
The implementation strongly resembles the one supported by all previous versions for Shapefile, and this isn't at all surprising because both them cover the same functional area.
SQL functions supporting GeoJSON
Exporting data to GeoJSON
SELECT ExportGeoJSON2('mytable', 'geom', '/home/sandro/mytable.geojson'); --------- 1234 SELECT ExportGeoJSON2('mytable', 'geom', '/home/sandro/mytable.geojson', 12); --------- 1234 SELECT ExportGeoJSON2('mytable', 'geom', '/home/sandro/mytable.geojson', 12, 1); --------- 1234 SELECT ExportGeoJSON2('mytable', 'geom', '/home/sandro/mytable.geojson', 12, 1, 0); --------- 1234 SELECT ExportGeoJSON2('mytable', 'geom', '/home/sandro/mytable.geojson', 12, 1, 0, 1); --------- 1234 SELECT ExportGeoJSON2('mytable', 'geom', '/home/sandro/mytable.geojson', 12, 1, 0, 1, 'LOWERCASE'); --------- 1234The SQL function ExportGeoJSON2() can directly export a whole Spatial Table (aka Layer) into an external GeoJSON file conformant to RFC 7946.
This function accepts the following arguments:
- table: (mandatory) name of the Spatial Table to be exported.
- geom_column: (mandatory) name of the Column containing the Geometries to be exported.
Can be NULL, and in this case the function will automatically determine the appropriate Column-name if the Table just contains a single Geometry, otherwise a failure will follow. - filename: (mandatory) absolute or relative path of the GeoJson file to be created (output file).
- precision: (optional) number of decimal digits to be used for coordinate values; the default setting is 8.
- lon_lat: (optional) this is a boolean flag:
- if set to FALSE all coordinates will be exported exactly as they are.
- if set to TRUE all coordinates will be automatically transformed (if required) into SRID=4326 WGS 84 as required by RFC 7946.
This is the default setting.
- M_coords: (optional) this too is a boolean flag:
- if set to FALSE eventual M-values will be simply ignored, as required by RFC 7946.
This is the default setting. - if set to TRUE eventual M-values will be exported into the GeoJSON file as extra-coordinates.
Note: the resulting output file could easily suffer from limited portability.
- if set to FALSE eventual M-values will be simply ignored, as required by RFC 7946.
- indented: (optional) yet another boolean flag:
- If set to TRUE the output file will be properly indented for enhanced human readability.
This is the default setting. - If set to FALSE the output file will be all in a single monolithic line without blank spaces or newlines (noticeably smaller).
- If set to TRUE the output file will be properly indented for enhanced human readability.
- colname_case: (optional) can assume one the following values:
- 'UPPER' or 'UPPERCASE': all attribute names will be inserted into the output file in full upper case.
- 'LOWER' or 'LOWERCASE': all attribute names will be inserted into the output file in full lower case.
This is the default setting. - 'SAME' or 'SAMECASE': all attribute names will be inserted into the output file preserving all upper- and lower-case letters exactly as they are defined in the corresponding column names.
- the return value is an INTEGER corresponding to the number of exported rows, or NULL in the case of failure or invalid arguments.
CaveatA similar function named ExportGeoJSON() was already supported by previous versions of SpatiaLite, but it was modelled against the old (pre-RFC) GeoJSON specification.ExportGeoJSON() is now DEPRECATED, and will remain to avoid breaking existing applications and scripts. It is strongly recommended to use ExportGeoJSON2() for any new development as full replacement conformant to RFC 7946. |
WarningThis SQL function open the door to many potential security issues, and thus is always disabled by default.Explicitly setting the external variable SPATIALITE_SECURITY=relaxed is absolutely required in order to enable this function. |
Importing data from GeoJSON
SELECT ImportGeoJSON('/home/sandro/somelayer.geojson', 'somelayer'); ----------- 99 SELECT ImportGeoJSON('/home/sandro/somelayer.geojson', 'somelayer', 'geom'); ----------- 99 SELECT ImportGeoJSON('/home/sandro/somelayer.geojson', 'somelayer', 'geom', 1); ----------- 99 SELECT ImportGeoJSON('/home/sandro/somelayer.geojson', 'somelayer', 'geom', 1, 3003); ----------- 99 SELECT ImportGeoJSON('/home/sandro/somelayer.geojson', 'somelayer', 'geom', 1, 3003, 'LOWERCASE'); ----------- 99The SQL function ImportGeoJSON() can directly create a new Spatial Table by importing data from an extenal GeoJSON file conformant to RFC 7946.
This function accepts the following arguments:
- filename: (mandatory) absolute or relative path of the GeoJson file to be imported (input file).
- table: (mandatory) name of the Spatial Table to be created.
- geom_column: (optional) name of the Geometry Column to be created.
If not explicitly specified or NULL a column named 'geometry' will be created by default. - spatial_index: (optional) this is a boolean flag:
- if set to TRUE a SpatialIndex supporting the Geometry Column will be automatically created.
- if set to FALSE no SpatialIndex will be created.
This is the default setting.
- srid: (optional):
- When it's a positive integer it corresponds to the SRID value to be assigned to all imported Geometries.
- If 0 or negative all imported Geometries will be assumed to be in SRID=4326 WGS 84 (geographic coordinates expressed as Longitudes and Latitudes) as required by RFC 7946.
This is the default setting.
- colname_case: (optional) can assume one the following values:
- 'UPPER' or 'UPPERCASE': all columns will be created into the table in full upper case.
- 'LOWER' or 'LOWERCASE': all columns will be created into the table in full lower case.
This is the default setting. - 'SAME' or 'SAMECASE': all columns will be created into the table preserving all upper- and lower-case letters exactly as they are defined in the corresponding GeoJSON attribute names.
- the return value is an INTEGER corresponding to the number of exported rows, or NULL in the case of failure or invalid arguments.
WarningThis SQL function open the door to many potential security issues, and thus is always disabled by default.Explicitly setting the external variable SPATIALITE_SECURITY=relaxed is absolutely required in order to enable this function. |
VirtualGeoJSON
CREATE VIRTUAL TABLE some_name USING VirtualGeoJSON( path, [ srid , [ colname_case ]] );A VirtualGeoJSON table is strictly similar to a VirtualShape table, the only difference between them being the file format of the underlaying datasource.
Except for this small but fundamental detail both them behave in exactly the same way.
- Just a single mandatory argument is required for creating a VirtualGeoJSON table; you are required to specify the absolute or relative path leading to the external GeoJSON file on the filesystem.
- You can explicitly specify the optional argument srid for out-of-the-spec GeoJSON files based on some arbitrary Coordinate Reference System.
By default VirtualGeoJSON will always assume that all Geometries should be assigned to SRID=4326 WGS 84 (longitudes and latitudes), accordingly to RFC 7946 specifications. - A further optional argument colname_case is supported, and it has exactly the same interpretations as in ImportGeoJSON()
GeoJSON support in spatialite_gui
The latest version of the GUI tool now supports GeoJSON in exactly the same way it supports Shapefiles.
Two new buttons have been added to the toolbar:
|
|
The side figure is an example of the dialog box that's used for setting options when creating a new Spatial Table by importing data from an external GeoJSON file. | |
The side figure is an example of the dialog box that's used for setting options when creating a VirtualGeoJSON Table. | |
For exporting a whole Spatial Table into an external GeoJSON file:
|
Back to 5.0.0-doc main page