Code Monkey home page Code Monkey logo

geojson's Introduction

๐ŸŒ GeoJSON

This is a Swift package for working with GeoJSON data. It contains necessary types conforming to Codable for easy de-/encoding of data.

The implementation of this package tries to follow the specification defined in RFC 7946 as closely as possible. One exception are Foreign Members, which - according to the spec - MAY be implemented, but are not supported at all by this package.

Usage

You can give this package a quick spin by cloning the repository and running swift run --repl inside it.

Encoding

Create a FeatureCollection or just a single Feature to create some GeoJSON data.

let features = FeatureCollection(features: [
    Feature(
        geometry: .point(Point(longitude: 125.6, latitude: 10.1)),
        properties: [
            "name": "Dinagat Island"
        ]
    )
])

let json = try JSONEncoder().encode(features)

Decoding

It works the same way in the other direction. Use a standard JSONDecoder to decode GeoJSON data into a fitting type.

let features = try JSONDecoder().decode(FeatureCollection.self, from: json)

If you don't know if the data to decode is a Feature or FeatureCollection, decode a value of type GeoJSONDocument, which handles both.

let document = try JSONDecoder().decode(GeoJSONDocument.self, from: json)
switch document {
case .feature(let feature):
    // ...
case .featureCollection(let featureCollection):
    // ...
}

Installation

This package is available via Swift Package Manager, add its clone URL to your project to get started.

geojson's People

Contributors

kiliankoe avatar

Watchers

James Cloos avatar  avatar

Forkers

deyvisross

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.