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.
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.
- RFC 7946 Compliance: Fully adheres to the RFC 7946 GeoJSON standard.
- Support for Common GeoJSON Types: Including
Point
,LineString
,Polygon
,MultiPoint
,MultiLineString
,MultiPolygon
,Feature
andFeatureCollection
. - 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.
To include this GeoJSON domain model in your Java project, follow these instructions:
- Java 8 or later
- Maven or Gradle as the build system
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>
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.
Once the library is installed, you can start working with the GeoJSON domain model in your Java application.
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);
}
}
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());
}
}
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()));
}
}
- Full API documentation is available in
todo
. - Refer to the
examples directory
for more sample use cases.
If you find any bugs or have a feature request, please open an issue on GitHub Issues.
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".
- Fork the Project
- Create your Feature Branch (git checkout -b feature/AmazingFeature)
- Commit your Changes (git commit -m 'feat: Add the AmazingFeature')
- Push to the Branch (git push origin feature/AmazingFeature)
- Open a Pull Request
This project is licensed under the Apache License.
Sincere Thanks to following open source community for their wonderful efforts to make our life much easier.
- SonarCloud - Static code analysis tool
- Renovate - Automated dependencies update
- OpenRewrite - Automated source code refactoring
- Jackson - JSON Parser Library
Ramachandran Nellaiyappan Website | Twitter | E-Mail
Give a ⭐️ if you like this project!