### Create and Use FeatureCollection in Java Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Demonstrates creating multiple Feature objects with properties and geometries, then assembling them into a FeatureCollection. Includes serialization to a GeoJSON string and iteration over the features. ```java import org.wololo.geojson.Feature; import org.wololo.geojson.FeatureCollection; import org.wololo.geojson.Point; import java.util.HashMap; import java.util.Map; // Create multiple features Feature[] features = new Feature[3]; // Feature 1: New York Point nyPoint = new Point(new double[]{-74.006, 40.7128}); Map nyProps = new HashMap<>(); nyProps.put("name", "New York"); nyProps.put("rank", 1); features[0] = new Feature("city-1", nyPoint, nyProps); // Feature 2: Los Angeles Point laPoint = new Point(new double[]{-118.2437, 34.0522}); Map laProps = new HashMap<>(); laProps.put("name", "Los Angeles"); laProps.put("rank", 2); features[1] = new Feature("city-2", laPoint, laProps); // Feature 3: Chicago Point chiPoint = new Point(new double[]{-87.6298, 41.8781}); Map chiProps = new HashMap<>(); chiProps.put("name", "Chicago"); chiProps.put("rank", 3); features[2] = new Feature("city-3", chiPoint, chiProps); // Create FeatureCollection FeatureCollection collection = new FeatureCollection(features); System.out.println("Number of features: " + collection.getFeatures().length); // Output: Number of features: 3 // Serialize to GeoJSON string String geoJsonString = collection.toString(); System.out.println(geoJsonString); // Output: {"type":"FeatureCollection","features":[{"type":"Feature","id":"city-1","geometry":{"type":"Point","coordinates":[-74.006,40.7128]},"properties":{"name":"New York","rank":1}},...]} // Iterate over features for (Feature f : collection.getFeatures()) { System.out.println(f.getProperties().get("name") + " (Rank: " + f.getProperties().get("rank") + ")"); } // Output: // New York (Rank: 1) // Los Angeles (Rank: 2) // Chicago (Rank: 3) ``` -------------------------------- ### Create GeoJSON Feature with Geometry and Properties Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Construct a GeoJSON Feature object by providing a geometry and a map of properties. Features can also include an optional ID. ```java import org.wololo.geojson.Feature; import org.wololo.geojson.Point; import org.wololo.geojson.Polygon; import java.util.HashMap; import java.util.Map; // Create a Feature with geometry and properties Point geometry = new Point(new double[]{-122.4194, 37.7749}); Map properties = new HashMap<>(); properties.put("city", "San Francisco"); properties.put("state", "California"); properties.put("population", 883305); properties.put("timezone", "America/Los_Angeles"); Feature feature = new Feature(geometry, properties); System.out.println(feature.toString()); // Output: {"type":"Feature","geometry":{"type":"Point","coordinates":[-122.4194,37.7749]},"properties":{"city":"San Francisco","timezone":"America/Los_Angeles","population":883305,"state":"California"}} // Create a Feature with an ID Feature featureWithId = new Feature("sf-001", geometry, properties); System.out.println("Feature ID: " + featureWithId.getId()); // Output: Feature ID: sf-001 System.out.println(featureWithId.toString()); // Output includes "id":"sf-001" in the JSON // Create a Feature with a complex polygon geometry double[][][] polygonCoords = new double[][][] {{ {-122.5, 37.7}, {-122.3, 37.7}, {-122.3, 37.85}, {-122.5, 37.85}, {-122.5, 37.7} }}; Polygon polygon = new Polygon(polygonCoords); Map areaProps = new HashMap<>(); areaProps.put("name", "San Francisco Bay Area"); areaProps.put("type", "metropolitan"); Feature areaFeature = new Feature(polygon, areaProps); System.out.println(areaFeature.toString()); ``` -------------------------------- ### Handle 3D Coordinates in GeoJSON Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Shows how to create and parse geometries containing z-coordinates (elevation/altitude). ```java import org.wololo.jts2geojson.GeoJSONReader; import org.wololo.jts2geojson.GeoJSONWriter; import org.locationtech.jts.geom.*; GeometryFactory factory = new GeometryFactory(); GeoJSONWriter writer = new GeoJSONWriter(); GeoJSONReader reader = new GeoJSONReader(); // Create a 3D point with elevation Coordinate coord3D = new Coordinate(-122.4194, 37.7749, 52.0); Point point3D = factory.createPoint(coord3D); org.wololo.geojson.GeoJSON geoJson = writer.write(point3D); System.out.println(geoJson.toString()); // Output: {"type":"Point","coordinates":[-122.4194,37.7749,52.0]} // Create a 3D LineString representing a flight path Coordinate[] flightPath = new Coordinate[] { new Coordinate(-122.4194, 37.7749, 0), // San Francisco (ground) new Coordinate(-118.2437, 34.0522, 10000), // Over LA (10km altitude) new Coordinate(-112.0740, 33.4484, 11000), // Over Phoenix (11km altitude) new Coordinate(-104.9903, 39.7392, 10500), // Over Denver (10.5km altitude) new Coordinate(-87.6298, 41.8781, 0) // Chicago (ground) }; LineString flight = factory.createLineString(flightPath); System.out.println(writer.write(flight).toString()); // Output includes z-coordinates for each point // Parse 3D GeoJSON back to JTS String json3D = "{\"type\":\"Point\",\"coordinates\":[-74.006,40.7128,15.5]}"; Geometry parsed = reader.read(json3D); Point parsedPoint = (Point) parsed; System.out.println("Z coordinate: " + parsedPoint.getCoordinate().getZ()); // Output: Z coordinate: 15.5 ``` -------------------------------- ### Add Maven Dependency Source: https://github.com/bjornharrtell/jts2geojson/blob/master/README.md Include this dependency in your project's pom.xml file to use the jts2geojson library. ```xml org.wololo jts2geojson 0.18.1 ``` -------------------------------- ### Perform JTS to GeoJSON Round-Trip Conversion Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Demonstrates converting JTS geometries to GeoJSON, creating feature collections, and parsing them back for spatial operations. ```java import org.wololo.jts2geojson.GeoJSONReader; import org.wololo.jts2geojson.GeoJSONWriter; import org.wololo.geojson.Feature; import org.wololo.geojson.FeatureCollection; import org.wololo.geojson.GeoJSON; import org.wololo.geojson.GeoJSONFactory; import org.locationtech.jts.geom.*; import java.util.*; // Initialize reader and writer GeoJSONReader reader = new GeoJSONReader(); GeoJSONWriter writer = new GeoJSONWriter(); GeometryFactory factory = new GeometryFactory(new PrecisionModel(), 4326); // Step 1: Create JTS geometries representing parks Coordinate[] park1Coords = new Coordinate[] { new Coordinate(-73.97, 40.77), new Coordinate(-73.95, 40.77), new Coordinate(-73.95, 40.80), new Coordinate(-73.97, 40.80), new Coordinate(-73.97, 40.77) }; Polygon park1 = factory.createPolygon(park1Coords); Coordinate[] park2Coords = new Coordinate[] { new Coordinate(-73.96, 40.78), new Coordinate(-73.94, 40.78), new Coordinate(-73.94, 40.81), new Coordinate(-73.96, 40.81), new Coordinate(-73.96, 40.78) }; Polygon park2 = factory.createPolygon(park2Coords); // Step 2: Convert JTS to GeoJSON and create Features List features = new ArrayList<>(); org.wololo.geojson.Geometry geojsonPark1 = writer.write(park1); Map props1 = new HashMap<>(); props1.put("name", "Central Park North"); props1.put("area_sqm", park1.getArea()); features.add(new Feature("park-1", geojsonPark1, props1)); org.wololo.geojson.Geometry geojsonPark2 = writer.write(park2); Map props2 = new HashMap<>(); props2.put("name", "Central Park South"); props2.put("area_sqm", park2.getArea()); features.add(new Feature("park-2", geojsonPark2, props2)); // Step 3: Create and serialize FeatureCollection GeoJSON featureCollection = writer.write(features); String geoJsonOutput = featureCollection.toString(); System.out.println("GeoJSON Output:"); System.out.println(geoJsonOutput); // Step 4: Parse GeoJSON back and perform spatial operations FeatureCollection parsed = (FeatureCollection) GeoJSONFactory.create(geoJsonOutput); Geometry[] geometries = new Geometry[parsed.getFeatures().length]; for (int i = 0; i < parsed.getFeatures().length; i++) { geometries[i] = reader.read(parsed.getFeatures()[i].getGeometry()); } // Calculate intersection Geometry intersection = geometries[0].intersection(geometries[1]); System.out.println("\nIntersection area: " + intersection.getArea()); System.out.println("Intersection WKT: " + intersection.toText()); // Calculate union Geometry union = geometries[0].union(geometries[1]); System.out.println("\nUnion area: " + union.getArea()); // Convert result back to GeoJSON org.wololo.geojson.Geometry intersectionGeoJson = writer.write(intersection); System.out.println("\nIntersection as GeoJSON: " + intersectionGeoJson.toString()); ``` -------------------------------- ### Handle GeoJSON Features and FeatureCollections Source: https://github.com/bjornharrtell/jts2geojson/blob/master/README.md Parse GeoJSON Features and FeatureCollections, extract geometries, or create new collections for serialization. ```java // parse Feature or FeatureCollection Feature feature = (Feature) GeoJSONFactory.create(json); FeatureCollection featureCollection = (FeatureCollection) GeoJSONFactory.create(json); // parse Geometry from Feature GeoJSONReader reader = new GeoJSONReader(); Geometry geometry = reader.read(feature.getGeometry()); geometry = reader.read(featureCollection.getFeatures()[0].getGeometry()); // create and serialize a FeatureCollection List features = new ArrayList(); Map properties = new HashMap(); features.add(new Feature(geometry, properties)); GeoJSONWriter writer = new GeoJSONWriter(); GeoJSON json = writer.write(features); ``` -------------------------------- ### Parse GeoJSON with custom GeometryFactory Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Allows specifying a custom GeometryFactory to control parameters like SRID and precision models. ```java import org.wololo.jts2geojson.GeoJSONReader; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.PrecisionModel; GeoJSONReader reader = new GeoJSONReader(); // Create a GeometryFactory with SRID 4326 (WGS84) PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING); GeometryFactory factory = new GeometryFactory(precisionModel, 4326); String json = "{\"type\":\"Point\",\"coordinates\":[-73.985428,40.748817]}"; Geometry geometry = reader.read(json, factory); System.out.println("SRID: " + geometry.getSRID()); // Output: SRID: 4326 System.out.println("Geometry type: " + geometry.getGeometryType()); // Output: Geometry type: Point ``` -------------------------------- ### GeoJSONWriter.write(List features) Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Converts a list of Feature objects into a GeoJSON FeatureCollection. ```APIDOC ## GeoJSONWriter.write(List features) ### Description Converts a list of Feature objects into a GeoJSON FeatureCollection, allowing for the inclusion of properties and metadata alongside geometry. ### Parameters #### Request Body - **features** (List) - Required - A list of GeoJSON Feature objects. ### Response #### Success Response (200) - **GeoJSON** (Object) - A GeoJSON FeatureCollection containing the provided features. ### Response Example { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-73.985428, 40.748817]}, "properties": {"name": "New York City", "population": 8336817} } ] } ``` -------------------------------- ### GeoJSONFactory.create(String json) Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Parses any GeoJSON string and returns the appropriate GeoJSON object type (Geometry, Feature, or FeatureCollection). This factory method is the primary entry point for parsing GeoJSON data and automatically handles type detection based on the "type" property in the JSON. ```APIDOC ## GeoJSONFactory.create(String json) ### Description Parses any GeoJSON string and returns the appropriate GeoJSON object type (Geometry, Feature, or FeatureCollection). This factory method is the primary entry point for parsing GeoJSON data and automatically handles type detection based on the "type" property in the JSON. ### Method Static method call ### Endpoint N/A (Java method) ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body - **json** (String) - Required - A JSON string representing GeoJSON data. ### Request Example ```java // Parse a simple geometry String pointJson = "{\"type\":\"Point\",\"coordinates\":[10.5,20.5]}"; GeoJSON geoJson = GeoJSONFactory.create(pointJson); Point point = (Point) geoJson; System.out.println("Type: " + point.getType()); // Parse a Feature with properties String featureJson = "{\"type\":\"Feature\",\"id\":\"building-1\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-73.985428,40.748817]},\"properties\":{\"name\":\"Empire State Building\",\"height\":443,\"floors\":102}}"; Feature feature = (Feature) GeoJSONFactory.create(featureJson); System.out.println("Feature ID: " + feature.getId()); Map props = feature.getProperties(); System.out.println("Name: " + props.get(\"name\")); System.out.println("Height: " + props.get(\"height\") + \"m\"); // Parse a FeatureCollection String collectionJson = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]},\"properties\":{\"id\":1}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1,1]},\"properties\":{\"id\":2}}]}"; FeatureCollection collection = (FeatureCollection) GeoJSONFactory.create(collectionJson); System.out.println("Number of features: " + collection.getFeatures().length); // Access individual features from collection GeoJSONReader reader = new GeoJSONReader(); for (Feature f : collection.getFeatures()) { org.locationtech.jts.geom.Geometry geom = reader.read(f.getGeometry()); System.out.println("Feature " + f.getProperties().get(\"id\") + ": " + geom.toText()); } ``` ### Response #### Success Response (200) - **GeoJSON** (Object) - The parsed GeoJSON object (Geometry, Feature, or FeatureCollection). #### Response Example ```json // Example for Point { "type": "Point", "coordinates": [10.5, 20.5] } // Example for Feature { "type": "Feature", "id": "building-1", "geometry": { "type": "Point", "coordinates": [-73.985428, 40.748817] }, "properties": { "name": "Empire State Building", "height": 443, "floors": 102 } } // Example for FeatureCollection { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [0, 0] }, "properties": { "id": 1 } }, { "type": "Feature", "geometry": { "type": "Point", "coordinates": [1, 1] }, "properties": { "id": 2 } } ] } ``` ``` -------------------------------- ### GeoJSONReader.read(String json, GeometryFactory geomFactory) Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Parses a GeoJSON string using a custom GeometryFactory for advanced configuration. ```APIDOC ## GeoJSONReader.read(String json, GeometryFactory geomFactory) ### Description Parses a GeoJSON string using a custom GeometryFactory, allowing control over coordinate precision, SRID, and other geometry construction parameters. ### Parameters #### Request Body - **json** (String) - Required - A valid GeoJSON string. - **geomFactory** (GeometryFactory) - Required - A custom JTS GeometryFactory instance. ### Response - **Geometry** (Object) - The resulting JTS Geometry object configured with the provided factory. ``` -------------------------------- ### Convert List of Features to GeoJSON FeatureCollection Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Use GeoJSONWriter.write(List) to convert a list of Feature objects into a GeoJSON FeatureCollection. This is useful for including properties and metadata with geographic data. ```java import org.wololo.jts2geojson.GeoJSONWriter; import org.wololo.geojson.Feature; import org.wololo.geojson.GeoJSON; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; GeometryFactory factory = new GeometryFactory(); GeoJSONWriter writer = new GeoJSONWriter(); List features = new ArrayList<>(); // Create first feature - a city point org.wololo.geojson.Point nycPoint = new org.wololo.geojson.Point(new double[]{-73.985428, 40.748817}); Map nycProps = new HashMap<>(); nycProps.put("name", "New York City"); ycProps.put("population", 8336817); ycProps.put("country", "USA"); features.add(new Feature(nycPoint, nycProps)); // Create second feature - another city point org.wololo.geojson.Point laPoint = new org.wololo.geojson.Point(new double[]{-118.243683, 34.052234}); Map laProps = new HashMap<>(); laProps.put("name", "Los Angeles"); laProps.put("population", 3979576); laProps.put("country", "USA"); features.add(new Feature(laPoint, laProps)); // Write as FeatureCollection GeoJSON featureCollection = writer.write(features); System.out.println(featureCollection.toString()); // Output: {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-73.985428,40.748817]},"properties":{"country":"USA","name":"New York City","population":8336817}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-118.243683,34.052234]},"properties":{"country":"USA","name":"Los Angeles","population":3979576}}]} ``` -------------------------------- ### Parse GeoJSON strings with default factory Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Uses the default GeometryFactory to convert various GeoJSON geometry types into JTS objects. ```java import org.wololo.jts2geojson.GeoJSONReader; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; GeoJSONReader reader = new GeoJSONReader(); // Parse a Point from GeoJSON string String pointJson = "{\"type\":\"Point\",\"coordinates\":[-73.985428,40.748817]}"; Geometry pointGeom = reader.read(pointJson); Point point = (Point) pointGeom; System.out.println("X: " + point.getX() + ", Y: " + point.getY()); // Output: X: -73.985428, Y: 40.748817 // Parse a Polygon from GeoJSON string String polygonJson = "{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[10,0],[10,10],[0,10],[0,0]]]}"; Geometry polygonGeom = reader.read(polygonJson); Polygon polygon = (Polygon) polygonGeom; System.out.println("Area: " + polygon.getArea()); // Output: Area: 100.0 System.out.println("Number of points: " + polygon.getNumPoints()); // Output: Number of points: 5 // Parse a MultiPolygon String multiPolygonJson = "{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[10,0],[10,10],[0,10],[0,0]]],[[[20,20],[30,20],[30,30],[20,30],[20,20]]]]}"; Geometry multiPolygon = reader.read(multiPolygonJson); System.out.println("Number of geometries: " + multiPolygon.getNumGeometries()); // Output: Number of geometries: 2 ``` -------------------------------- ### Parse GeoJSON String with GeoJSONFactory Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Use GeoJSONFactory.create() to parse any GeoJSON string into its corresponding Java object. This method automatically detects the GeoJSON type. ```java import org.wololo.geojson.GeoJSONFactory; import org.wololo.geojson.GeoJSON; import org.wololo.geojson.Feature; import org.wololo.geojson.FeatureCollection; import org.wololo.geojson.Point; import org.wololo.jts2geojson.GeoJSONReader; import java.util.Map; // Parse a simple geometry String pointJson = "{\"type\":\"Point\",\"coordinates\":[10.5,20.5]}"; GeoJSON geoJson = GeoJSONFactory.create(pointJson); Point point = (Point) geoJson; System.out.println("Type: " + point.getType()); // Output: Type: Point // Parse a Feature with properties String featureJson = "{\"type\":\"Feature\",\"id\":\"building-1\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-73.985428,40.748817]},\"properties\":{\"name\":\"Empire State Building\",\"height\":443,\"floors\":102}}"; Feature feature = (Feature) GeoJSONFactory.create(featureJson); System.out.println("Feature ID: " + feature.getId()); // Output: Feature ID: building-1 Map props = feature.getProperties(); System.out.println("Name: " + props.get("name")); // Output: Name: Empire State Building System.out.println("Height: " + props.get("height") + "m"); // Output: Height: 443m // Parse a FeatureCollection String collectionJson = "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[0,0]},\"properties\":{\"id\":1}},{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[1,1]},\"properties\":{\"id\":2}}]}"; FeatureCollection collection = (FeatureCollection) GeoJSONFactory.create(collectionJson); System.out.println("Number of features: " + collection.getFeatures().length); // Output: Number of features: 2 // Access individual features from collection GeoJSONReader reader = new GeoJSONReader(); for (Feature f : collection.getFeatures()) { org.locationtech.jts.geom.Geometry geom = reader.read(f.getGeometry()); System.out.println("Feature " + f.getProperties().get("id") + ": " + geom.toText()); } // Output: Feature 1: POINT (0 0) // Output: Feature 2: POINT (1 1) ``` -------------------------------- ### Convert JTS Geometry to GeoJSON and Back Source: https://github.com/bjornharrtell/jts2geojson/blob/master/README.md Use GeoJSONWriter to serialize JTS geometries and GeoJSONReader to deserialize them. ```java GeoJSONWriter writer = new GeoJSONWriter(); GeoJSON json = writer.write(geometry); String jsonstring = json.toString(); GeoJSONReader reader = new GeoJSONReader(); Geometry geometry = reader.read(json); ``` -------------------------------- ### GeoJSONReader.read(GeoJSON geoJSON) Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Converts an already parsed GeoJSON object into a JTS Geometry. ```APIDOC ## GeoJSONReader.read(GeoJSON geoJSON) ### Description Converts a GeoJSON object (already parsed) into a JTS Geometry. This is useful when working with GeoJSON objects obtained from Feature or FeatureCollection parsing. ### Parameters #### Request Body - **geoJSON** (GeoJSON) - Required - An already parsed GeoJSON object. ### Response - **Geometry** (Object) - The resulting JTS Geometry object. ``` -------------------------------- ### GeoJSONReader.read(String json) Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Parses a GeoJSON string into a JTS Geometry object using a default GeometryFactory. ```APIDOC ## GeoJSONReader.read(String json) ### Description Parses a GeoJSON string and converts it into a JTS Geometry object. This method automatically detects the geometry type from the JSON and creates the appropriate JTS geometry using a default GeometryFactory with floating-point precision. ### Parameters #### Request Body - **json** (String) - Required - A valid GeoJSON string representation of a geometry. ### Response - **Geometry** (Object) - The resulting JTS Geometry object. ``` -------------------------------- ### GeoJSONWriter.write(Geometry geometry) Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Converts a JTS Geometry object into its corresponding GeoJSON representation. ```APIDOC ## GeoJSONWriter.write(Geometry geometry) ### Description Converts a JTS Geometry object to its GeoJSON representation. This method handles all standard geometry types including Point, LineString, Polygon, and their multi-variants. ### Parameters #### Request Body - **geometry** (Geometry) - Required - The JTS Geometry object to be converted. ### Response #### Success Response (200) - **GeoJSON** (Object) - The resulting GeoJSON representation of the geometry. ### Response Example { "type": "Point", "coordinates": [-73.985428, 40.748817] } ``` -------------------------------- ### Convert JTS Geometry to GeoJSON Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Use GeoJSONWriter.write(Geometry) to convert JTS Geometry objects (Point, Polygon, etc.) into their GeoJSON string representation. Ensure necessary JTS and GeoJSON classes are imported. ```java import org.wololo.jts2geojson.GeoJSONWriter; import org.wololo.geojson.GeoJSON; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; GeometryFactory factory = new GeometryFactory(); GeoJSONWriter writer = new GeoJSONWriter(); // Convert a Point to GeoJSON Point point = factory.createPoint(new Coordinate(-73.985428, 40.748817)); GeoJSON pointJson = writer.write(point); String jsonString = pointJson.toString(); // Output: {"type":"Point","coordinates":[-73.985428,40.748817]} ``` ```java import org.wololo.jts2geojson.GeoJSONWriter; import org.wololo.geojson.GeoJSON; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; GeometryFactory factory = new GeometryFactory(); GeoJSONWriter writer = new GeoJSONWriter(); // Convert a Polygon to GeoJSON Coordinate[] coords = new Coordinate[] { new Coordinate(0, 0), new Coordinate(10, 0), new Coordinate(10, 10), new Coordinate(0, 10), new Coordinate(0, 0) }; Polygon polygon = factory.createPolygon(coords); GeoJSON polygonJson = writer.write(polygon); System.out.println(polygonJson.toString()); // Output: {"type":"Polygon","coordinates":[[[0.0,0.0],[10.0,0.0],[10.0,10.0],[0.0,10.0],[0.0,0.0]]]} ``` ```java import org.wololo.jts2geojson.GeoJSONWriter; import org.wololo.geojson.GeoJSON; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; import org.locationtech.jts.geom.Polygon; GeometryFactory factory = new GeometryFactory(); GeoJSONWriter writer = new GeoJSONWriter(); // Convert a Polygon with a hole Coordinate[] shell = new Coordinate[] { new Coordinate(0, 0), new Coordinate(20, 0), new Coordinate(20, 20), new Coordinate(0, 20), new Coordinate(0, 0) }; Coordinate[] hole = new Coordinate[] { new Coordinate(5, 5), new Coordinate(15, 5), new Coordinate(15, 15), new Coordinate(5, 15), new Coordinate(5, 5) }; Polygon polygonWithHole = factory.createPolygon( factory.createLinearRing(shell), new org.locationtech.jts.geom.LinearRing[] { factory.createLinearRing(hole) } ); System.out.println(writer.write(polygonWithHole).toString()); // Output includes both exterior ring and interior hole ring ``` -------------------------------- ### Convert parsed GeoJSON objects Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt Converts existing GeoJSON objects, such as those extracted from Features, directly into JTS geometries. ```java import org.wololo.jts2geojson.GeoJSONReader; import org.wololo.geojson.GeoJSONFactory; import org.wololo.geojson.Feature; import org.wololo.geojson.Geometry; import org.locationtech.jts.geom.Point; GeoJSONReader reader = new GeoJSONReader(); // Parse a Feature and extract its geometry String featureJson = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[-73.985428,40.748817]},\"properties\":{\"name\":\"Empire State Building\"}}"; Feature feature = (Feature) GeoJSONFactory.create(featureJson); // Get the GeoJSON geometry from the feature Geometry geoJsonGeometry = feature.getGeometry(); // Convert GeoJSON geometry to JTS geometry org.locationtech.jts.geom.Geometry jtsGeometry = reader.read(geoJsonGeometry); Point point = (Point) jtsGeometry; System.out.println("Coordinates: " + point.getX() + ", " + point.getY()); // Output: Coordinates: -73.985428, 40.748817 ``` -------------------------------- ### Feature Class Source: https://context7.com/bjornharrtell/jts2geojson/llms.txt The Feature class represents a GeoJSON Feature object, combining a geometry with arbitrary properties. Features can optionally include an ID field and are the fundamental building blocks for GeoJSON data with metadata. ```APIDOC ## Feature Class ### Description The Feature class represents a GeoJSON Feature object, combining a geometry with arbitrary properties. Features can optionally include an ID field and are the fundamental building blocks for GeoJSON data with metadata. ### Method Constructors and methods of the Feature class. ### Endpoint N/A (Java class) ### Parameters N/A for class description. See constructors for specific parameters. ### Request Example ```java // Create a Feature with geometry and properties Point geometry = new Point(new double[]{-122.4194, 37.7749}); Map properties = new HashMap<>(); properties.put("city", "San Francisco"); properties.put("state", "California"); properties.put("population", 883305); properties.put("timezone", "America/Los_Angeles"); Feature feature = new Feature(geometry, properties); System.out.println(feature.toString()); // Create a Feature with an ID Feature featureWithId = new Feature("sf-001", geometry, properties); System.out.println("Feature ID: " + featureWithId.getId()); System.out.println(featureWithId.toString()); // Create a Feature with a complex polygon geometry double[][][] polygonCoords = new double[][][] {{ {-122.5, 37.7}, {-122.3, 37.7}, {-122.3, 37.85}, {-122.5, 37.85}, {-122.5, 37.7} }}; Polygon polygon = new Polygon(polygonCoords); Map areaProps = new HashMap<>(); areaProps.put("name", "San Francisco Bay Area"); areaProps.put("type", "metropolitan"); Feature areaFeature = new Feature(polygon, areaProps); System.out.println(areaFeature.toString()); ``` ### Response #### Success Response (200) - **Feature** (Object) - A GeoJSON Feature object. #### Response Example ```json // Example for Feature without ID { "type": "Feature", "geometry": { "type": "Point", "coordinates": [-122.4194, 37.7749] }, "properties": { "city": "San Francisco", "state": "California", "population": 883305, "timezone": "America/Los_Angeles" } } // Example for Feature with ID { "type": "Feature", "id": "sf-001", "geometry": { "type": "Point", "coordinates": [-122.4194, 37.7749] }, "properties": { "city": "San Francisco", "state": "California", "population": 883305, "timezone": "America/Los_Angeles" } } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.