Skip to content

A Java library for working with GeoJSON data, compliant with RFC 7946. GeoJSON4j provides easy-to-use utilities for creating, parsing, and serializing GeoJSON objects, such as Points, LineStrings, Polygons, and FeatureCollections, while fully adhering to the official GeoJSON specification (RFC 7946).

License

Notifications You must be signed in to change notification settings

nramc/geojson4j

Repository files navigation

GitHub Actions Workflow Status Quality Gate Status Coverage Maven Central Version Badge

geojson4j [GeoJSON for Java]

GeoJSON4j is a lightweight, easy-to-use Java library for working with GeoJSON data, fully compliant with RFC-7946. It provides utilities to create, parse, and serialize GeoJSON objects, such as Point, LineString, Polygon, and FeatureCollection, to simplify geospatial data handling in Java applications.


Table of Contents

Introduction

This project provides a Java-based domain model for working with GeoJSON data, ensuring compliance with RFC 7946. GeoJSON is a format for encoding a variety of geographic data structures using JSON. This domain model simplifies the use of GeoJSON in Java applications by providing a strong, object-oriented approach to geospatial data.

The domain model supports GeoJSON geometry types, including:

  • Point
  • LineString
  • Polygon
  • MultiPoint
  • MultiLineString
  • MultiPolygon
  • GeometryCollection
  • Feature
  • FeatureCollection

Additionally, it includes methods for validation, serialization, and conversion between GeoJSON and other geospatial formats, while ensuring full compliance with RFC 7946.

Features

  • RFC 7946 Compliance: Fully adheres to the RFC 7946 GeoJSON standard.
  • Support for Common GeoJSON Types: Including Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, Feature and FeatureCollection.
  • Serialization/Deserialization: Easily convert between Java objects and GeoJSON format using Jackson.
  • Validation: Built-in validation to ensure that GeoJSON data is well-formed and complies with the specification.
  • Extensible: The model is designed to be extensible for custom use cases and geospatial operations.

Installation

To include this GeoJSON domain model in your Java project, follow these instructions:

Requirements

  • Java 8 or later
  • Maven or Gradle as the build system

Maven Installation

If you're using Maven, add the following dependency to your pom.xml file:

<dependency>
    <groupId>io.github.nramc</groupId>
    <artifactId>geojson4j</artifactId>
    <version>${geojson4j.version}</version>
</dependency>

Gradle Installation

For Gradle, add this line to your build.gradle file:

implementation 'io.github.nramc:geojson4j:1.0.0'

Then, run mvn install or gradle build to download the dependencies.

Usage

Once the library is installed, you can start working with the GeoJSON domain model in your Java application.

Serialization

import com.github.nramc.geojson.domain.Point;
import com.fasterxml.jackson.databind.ObjectMapper;

public class GeoJsonExample {
    public static void main(String[] args) throws Exception {
        // Create a Point object with longitude & latitude
        Point point = Point.of(60.8, 20.5);

        // Serialize the Point object to GeoJSON format
        ObjectMapper mapper = new ObjectMapper();
        String geoJson = mapper.writeValueAsString(point);

        // Print GeoJSON
        System.out.println(geoJson);
    }
}

Deserialization

import com.github.nramc.geojson.domain.Point;
import com.fasterxml.jackson.databind.ObjectMapper;

public class GeoJsonExample {
    public static void main(String[] args) throws Exception {
        // Create a Point object with longitude & latitude
        String geoJson = """
                { "type": "Point", "coordinates": [60.8, 20.5] }""";

        // Deserialize the GeoJSON string to Point object
        ObjectMapper mapper = new ObjectMapper();
        Point point = mapper.readValue(geoJson, Point.class);

        // Print Coordinates
        System.out.println("long: %f lat:%f", point.getCoordinates().getLongitude(), point.getCoordinates().getLatitude());
    }
}

Validations

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.nramc.geojson.domain.GeoJson;


public class Main {

    public static void main(String[] args) throws JsonProcessingException {

        String geoJsonContent = "{ \"type\": \"Point\", \"coordinates\": [102.0, 0.5] }";

        // Deserialize the GeoJSON string to Point object
        ObjectMapper mapper = new ObjectMapper();
        GeoJson geoJson = mapper.readValue(geoJsonContent, GeoJson.class);

        // check whether the geoJson valid or not
        System.out.printf("is it valid geojson ? %s", geoJson.isValid() ? "Yes" : "No");

        // to get detailed information about validation failure 
        ValidationResult validationResult = geoJson.validate();
        System.out.printf("is it valid geojson ? %s%n", !validationResult.hasErrors() ? "Yes" : "No");
        validationResult.getErrors().forEach(error -> System.out.printf("Error Field:[%s] Error Key:[%s] Error Message:[%s]%n", error.getField(), error.getKey(), error.getMessage()));
    }
}

Documentation

  • Full API documentation is available in todo.
  • Refer to the examples directory for more sample use cases.

Issues

If you find any bugs or have a feature request, please open an issue on GitHub Issues.

Contributing

Any contributions you make are greatly appreciated.

If you like the project and have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'feat: Add the AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the Apache License.

Credits

Sincere Thanks to following open source community for their wonderful efforts to make our life much easier.

Contact

Ramachandran Nellaiyappan Website | Twitter | E-Mail

Show your support

Give a ⭐️ if you like this project!

About

A Java library for working with GeoJSON data, compliant with RFC 7946. GeoJSON4j provides easy-to-use utilities for creating, parsing, and serializing GeoJSON objects, such as Points, LineStrings, Polygons, and FeatureCollections, while fully adhering to the official GeoJSON specification (RFC 7946).

Topics

Resources

License

Stars

Watchers

Forks

Languages