Skip to main content
Version Build Status

A mappyfile plugin to convert GeoJSON to inline Mapfile features. Useful for adding dynamically created features (from web services, user created features, and other external data sources) to a map, or to quickly visualise a geometry.

Note - to display entire GeoJSON files MapServer can be configured to read GeoJSON as an input OGR source using the GeoJSON driver.

import geojson
import mappyfile
# import directly
import mappyfile_geojson
# can also be imported as plugin using
from mappyfile.plugins import mappyfile_geojson

gj = geojson.load(fn)
l = mappyfile_geojson.convert(gj)
print(mappyfile.dumps(l))

Converts the following JSON:

{
  "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
  }
}

to the following Mapfile LAYER:

LAYER
    EXTENT 102 0 105 1
    STATUS ON
    TYPE LINE
    PROCESSING "ITEMS=prop0,prop1"
    FEATURE
        ITEMS "value0;0.0"
        POINTS
            102.0 0.0
            103.0 1.0
            104.0 0.0
            105.0 1.0
        END
    END
END

Demo

An example of using the plugin with mappyfile can be found in example.py along with an example.map.

https://raw.githubusercontent.com/geographika/mappyfile-geojson/master/polygon.png

A further example, creating images for each of the test cases using mapscript is available at create_images.py.

The sample output images are in the images folder.

Requirements

  • Python 3.8+

  • mappyfile (the plugin can be used on its own but will create a dictionary object structured to use within mappyfile). Installing mappyfile should be done separately.

Installation

Note installing the mappyfile-geojson plugin will automatically install the required dependency geojson.

pip install mappyfile
pip install mappyfile-geojson

Notes

  • Can calculate extent of input features, with an optional buffer (by passing an extent_buffer to the convert function)

  • Multipart features are supported

  • Coordinate sequences with Z values are supported, but Z values are ignored as they are not supported in Mapserver inline features.

  • As a MapServer LAYER only supports a single geometry type, all features in the GeoJSON file should also be of the same type (however a mix of multipart and non-multipart features is supported e.g. LineString and MultiLineString)

  • Nested JSON properties are not supported:

    "properties": {
        "prop0": "value0",
        "prop1": { "this": "that" }
    }

    Will become:

    ITEMS "value0;{u'this': u'that'}"

Releases

1.0 (02/10/2023)

  • Drop support for Python 2.7

  • Update of GeoJSON and mappyfile dependencies

  • Code reformatted using black

0.4 (09/02/2020)

  • Automated Windows testing

  • Automated release process

  • Set geojson dependency version

  • Fix failing tests due to precision issue differences between py2 and py3

  • Use integers for layer EXTENT where possible e.g. 5 instead of 5.0

0.3 (29/08/2018)

  • Add support for MultiPoint, MultiLineString, and MultiPolygon

  • Allow coordinates with Z values (previously these would crash the script)

  • Updated README

0.2 (15/02/2018)

  • Unicode support

0.1 (06/02/2018)

  • Initial release

Author

Release files for mappyfile-geojson 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mappyfile-geojson 1.0.0
File Size Uploaded
mappyfile-geojson-1.0.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mappyfile-geojson 1.0.0
File Interpreter ABI Platform
mappyfile_geojson-1.0.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.5 kB

Release files / mappyfile-geojson-1.0.0.tar.gz

Download URL mappyfile-geojson-1.0.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5b14b7378001dd96cf6728230cc78f421ac8744d1537a19dec7a0ce2492c1207
BLAKE2b-256 checksum
How to use checksums
7bc39c7b5d00cf8660e89c493acd812051b17b4d11fceddbf18408b83c6cb52d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release files / mappyfile_geojson-1.0.0-py2.py3-none-any.whl

Download URL mappyfile_geojson-1.0.0-py2.py3-none-any.whl
Size 5.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
ed4155afb91fcff622918e9203941c6dab5fe39d9c4f212b8f60c6cfc243b865
BLAKE2b-256 checksum
How to use checksums
c56cc1e2e028a098be25de4ccf2a4f5dc389845815f5691215fc874acba63e0c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.4.0

5 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

1 release file

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page