Git Product home page Git Product logo

zig_geojson's Introduction

zig_geojson

A library for parsing GeoJSON in zig.

Example

const json =
    \\{
    \\   "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"
    \\           }
    \\       }
    \\   }]
    \\}
;

var geojson = Parser.parse(json, std.testing.allocator) catch |err| {
    std.debug.warn("Could not parse geojson! {}", .{err});
    return;
};
defer geojson.deinit();

// switch on the content if you don't already know the type
switch (geojson.content) {
    .feature => |feature| std.debug.warn("It's a Feature!\n", .{}),
    .feature_collection => |features| std.debug.warn("It's a FeatureCollection!\n", .{}),
    .geometry => |geometry| std.debug.warn("It's a Geometry!\n", .{}),
}

// there are helper methods `featureCollection()`, `feature()`, and `geometry()`, returning optionals
if (geojson.featureCollection()) |collection| {
    std.debug.warn("FeatureCollection contains {} features\n", .{collection.len});

    for (collection) |feature, idx| {
        std.debug.warn("{}: It's a {s} => ", .{ idx, @tagName(feature.geometry) });
        switch (feature.geometry) {
            .point => |value| std.debug.warn("[{d}, {d}]\n", .{ value[0], value[1] }),
            .multi_point => |value| std.debug.warn("containing {} points\n", .{value.len}),
            .line_string => |value| std.debug.warn("containing {} points\n", .{value.len}),
            .multi_line_string => |value| std.debug.warn("containing {} lineStrings\n", .{value.len}),
            .polygon => |value| std.debug.warn("containing {} rings\n", .{value.len}),
            .multi_polygon => |value| std.debug.warn("containing {} polygons\n", .{value.len}),
            .geometry_collection => |value| std.debug.warn("containing {} geometries\n", .{value.len}),
            .@"null" => continue,
        }
    }

    // accessing properties (safe)
    const feature = collection[0];
    if (feature.properties) |properties| {
        if (properties.get("prop0")) |value| {
            std.debug.warn("Property: 'prop0' => {}\n", .{value});
        }
    }

    // or unsafe
    const value = collection[1].properties.?.get("prop1").?;
    std.debug.warn("Property: 'prop1' => {}\n", .{value});
}
It's a FeatureCollection!
FeatureCollection contains 3 features
0: It's a point => [102, 0.5]
1: It's a line_string => containing 4 points
2: It's a polygon => containing 1 rings
Property: 'prop0' => PropertyValue{ .string = value0 }
Property: 'prop1' => PropertyValue{ .float = 0.0e+00 }

License

MIT

zig_geojson's People

Contributors

sebastiankeller avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.