{"id":15982897,"url":"https://github.com/ledfan/kotlin-geojsondsl","last_synced_at":"2025-05-17T09:42:39.268Z","repository":{"id":48688896,"uuid":"199015144","full_name":"LEDfan/kotlin-geojsondsl","owner":"LEDfan","description":null,"archived":false,"fork":false,"pushed_at":"2021-07-14T05:27:16.000Z","size":59,"stargazers_count":0,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-18T02:15:16.616Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LEDfan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-26T12:45:31.000Z","updated_at":"2020-07-18T14:17:21.000Z","dependencies_parsed_at":"2022-09-08T08:10:35.499Z","dependency_job_id":null,"html_url":"https://github.com/LEDfan/kotlin-geojsondsl","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LEDfan%2Fkotlin-geojsondsl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LEDfan%2Fkotlin-geojsondsl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LEDfan%2Fkotlin-geojsondsl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LEDfan%2Fkotlin-geojsondsl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LEDfan","download_url":"https://codeload.github.com/LEDfan/kotlin-geojsondsl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254595813,"owners_count":22097640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-08T01:23:10.965Z","updated_at":"2025-05-17T09:42:38.378Z","avatar_url":"https://github.com/LEDfan.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"Kotlin GeoJSON DSL\n==================\n\n[![Build Status](https://travis-ci.com/LEDfan/kotlin-geojsondsl.svg?token=AhWiySeGEDkQfLDToshu\u0026branch=master)](https://travis-ci.com/LEDfan/kotlin-geojsondsl)\n\nThis is a small Kotlin library to easily generate [GeoJSON](https://geojson.org/) using a DSL.\nThe library uses the [Klaxon](https://github.com/cbeust/klaxon) library.\n\n\n## Minimal example\n\nFor example, the following Kotlin code:\n```kotlin\nprintln(feature {\n    withGeometry {\n        point(Coordinate(4.3516970, 50.8465573))\n        withProperty(\"city\", \"Brussels\")\n        withProperty(\"country\", \"Belgium\")\n    }\n}.toJson().toJsonString(prettyPrint = true))\n```\nwill print:\n```json\n{\n  \"type\": \"Feature\",\n  \"geometry\": {\n    \"type\": \"Point\",\n    \"coordinates\": [50.8465573, 4.351697]\n  },\n  \"properties\": {\n    \"country\": \"Belgium\",\n    \"city\": \"Brussels\"\n  }\n}\n```\n\n## Coordinate\n\nThe library does not depend on any specific geometry library. In order to represent a coordinate, the library uses the `ICoordinate` interface:\n```kotlin\ninterface ICoordinate {\n    fun toJson(): JsonArray\u003cDouble\u003e\n}\n```\nBefore you can use the library, you have to implement this interface, for example using:\n```kotlin\ndata class Coordinate(val lat: Double, val lon: Double) : ICoordinate {\n    override fun toJson(): JsonArray\u003cDouble\u003e {\n        return json {\n            array(\n                    lon,\n                    lat\n            )\n        } as JsonArray\u003cDouble\u003e\n    }\n}\n```\n\n### Feature\nTo create a feature object, simply call the `feature` function with a [trailling lambda](https://kotlinlang.org/docs/reference/lambdas.html#passing-a-lambda-to-the-last-parameter). In this lambda the following functions are available:\n\n - `withGeometry` add a geometry to the feature\n - `withGeometryCollection` add a collection of geometries to the feature\n - `withProperty` add a property to the feature. The value can either be `Any` or a trailling lambda in which you can call the `array` or `obj` function os klaxon.\n - `withId` add a id of type string or number to the feature\n\n[Above](#minimal-example) you already find an example of `withGeometry`.\nThe following is an example of a GeometryCollection:\n```kotlin\nprintln(feature {\n    withGeometryCollection {\n        point(Coordinate(1.0, 2.0))\n        lineString {\n            withCoordinate(Coordinate(1.0, 2.0))\n            withCoordinate(Coordinate(3.0, 4.0))\n        }\n    }\n}.toJson().toJsonString(prettyPrint = true))\n```\nwhich prints:\n```json\n{\n  \"type\": \"Feature\",\n  \"geometry\": {\n    \"type\": \"GeometryCollection\",\n    \"geometries\": [\n      {\n        \"type\": \"Point\",\n        \"coordinates\": [\n          2.0,\n          1.0\n        ]\n      },\n      {\n        \"type\": \"LineString\",\n        \"coordinates\": [\n          [\n            2.0,\n            1.0\n          ],\n          [\n            4.0,\n            3.0\n          ]\n        ]\n      }\n    ]\n  },\n  \"properties\": {}\n}\n```\n\nExamples of `withProperty`:\n```kotlin\nfeature {\n    withGeometry {\n        point(Coordinate(10.0, 10.0))\n    }\n    withProperty(\"test1\", \"1\") // simple property\n    withProperty(\"test2\", \"2\") // simple property\n    withProperty(\"test3\") {\n        // complex json array property\n        array(1, 2, 3)\n    }\n    withProperty(\"test4\") {\n        // complex json object property\n        obj(\"a\" to \"b\", \"b\" to array(1, 2))\n    }\n}.toJson().toJsonString(prettyPrint = true))\n```\nwhich prints\n```json\n{\n  \"type\": \"Feature\",\n  \"geometry\": {\n    \"type\": \"Point\",\n    \"coordinates\": [\n      10.0,\n      10.0\n    ]\n  },\n  \"properties\": {\n    \"test4\": {\n      \"a\": \"b\",\n      \"b\": [\n        1,\n        2\n      ]\n    },\n    \"test2\": \"2\",\n    \"test3\": [\n      1,\n      2,\n      3\n    ],\n    \"test1\": \"1\"\n  }\n}\n```\n\n### FeatureCollection\nA featureCollection can be created similar to a feature by calling the `featureCollection` function. For example:\n```kotlin\nfeatureCollection {\n    withFeature {\n        withId(\"test\")\n        withGeometry {\n            point(Coordinate(1.0, 1.0))\n        }\n    }\n    withFeature {\n        withId(\"test\")\n        withGeometryCollection {\n            multiPoint {\n                withCoordinate(Coordinate(2.0, 3.0))\n                withCoordinate(Coordinate(4.0, 5.0))\n            }\n            point(Coordinate(5.0, 6.0))\n        }\n    }\n}.toJson().toJsonString(prettyPrint = true))\n```\n```json\n{\n  \"type\": \"FeatureCollection\",\n  \"features\": [\n    {\n      \"type\": \"Feature\",\n      \"id\": \"test\",\n      \"geometry\": {\n        \"type\": \"Point\",\n        \"coordinates\": [\n          1.0,\n          1.0\n        ]\n      },\n      \"properties\": {}\n    },\n    {\n      \"type\": \"Feature\",\n      \"id\": \"test\",\n      \"geometry\": {\n        \"type\": \"GeometryCollection\",\n        \"geometries\": [\n          {\n            \"type\": \"MultiPoint\",\n            \"coordinates\": [\n              [\n                3.0,\n                2.0\n              ],\n              [\n                5.0,\n                4.0\n              ]\n            ]\n          },\n          {\n            \"type\": \"Point\",\n            \"coordinates\": [\n              6.0,\n              5.0\n            ]\n          }\n        ]\n      },\n      \"properties\": {}\n    }\n  ]\n}\n```\n\n### Point\n```kotlin\nfeature {\n    withGeometry {\n        point(Coordinate(1.0, 1.0))\n    }\n}\n```\n```json\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[10.0,10.0]},\"properties\":{}}\n```\n\n### MultiPoint\n```kotlin\nfeature {\n    withGeometry {\n        multiPoint {\n            // only one coordinate is allowed\n            withCoordinate(Coordinate(10.0, 10.0))\n            withCoordinate(Coordinate(12.0, 12.0))\n        }\n    }\n}\n```\n```json\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,10.0],[12.0,12.0]]},\"properties\":{}}\n```\n\n### LineString\n```kotlin\nfeature {\n    withGeometry {\n        lineString {\n            withCoordinate(Coordinate(10.0, 10.0))\n            withCoordinate(Coordinate(10.0, 10.0))\n        }\n    }\n}\n```\n```json\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[10.0,10.0],[10.0,10.0]]},\"properties\":{}}\n```\n\n\n### MultiLineString\n```kotlin\nfeature {\n    withGeometry {\n        multiLineString {\n            lineString {\n                withCoordinate(Coordinate(1.1, 2.2))\n                withCoordinate(Coordinate(3.3, 4.4))\n            }\n            lineString {\n                withCoordinate(Coordinate(5.5, 6.6))\n                withCoordinate(Coordinate(7.7, 8.8))\n            }\n        }\n    }\n}\n```\n```json\n{\"type\":\"Feature\",\"geometry\":{\"type\":\"MultiLineString\",\"coordinates\":[[[2.2,1.1],[4.4,3.3]],[[6.6,5.5],[8.8,7.7]]]},\"properties\":{}}\n```\n\n### Polygon\n```kotlin\nfeature {\n    withGeometry {\n        polygon {\n            ring {\n                withCoordinate(Coordinate(1.1, 2.2))\n                withCoordinate(Coordinate(3.3, 4.4))\n                withCoordinate(Coordinate(3.3, 4.6))\n                withCoordinate(Coordinate(1.1, 2.2))\n            }\n            ring {\n                withCoordinate(Coordinate(0.5, 3.14))\n                withCoordinate(Coordinate(5.6, 4.2))\n                withCoordinate(Coordinate(8.8, 7.6))\n                withCoordinate(Coordinate(4.2, 5.2))\n                withCoordinate(Coordinate(0.5, 3.14))\n            }\n        }\n    }\n}\n```\n```json\n{\n  \"type\": \"Feature\",\n  \"geometry\": {\n    \"type\": \"Polygon\",\n    \"coordinates\": [\n      [\n        [\n          2.2,\n          1.1\n        ],\n        [\n          4.4,\n          3.3\n        ],\n        [\n          4.6,\n          3.3\n        ],\n        [\n          2.2,\n          1.1\n        ]\n      ],\n      [\n        [\n          3.14,\n          0.5\n        ],\n        [\n          4.2,\n          5.6\n        ],\n        [\n          7.6,\n          8.8\n        ],\n        [\n          5.2,\n          4.2\n        ],\n        [\n          3.14,\n          0.5\n        ]\n      ]\n    ]\n  },\n  \"properties\": {}\n}\n```\n\n### MultiPolygon\n```kotlin\nfeature {\n    withGeometry {\n        multiPolygon {\n            polygon {\n                ring {\n                    withCoordinate(Coordinate(1.1, 2.2))\n                    withCoordinate(Coordinate(3.3, 4.4))\n                    withCoordinate(Coordinate(3.3, 4.6))\n                    withCoordinate(Coordinate(1.1, 2.2))\n                }\n                ring {\n                    withCoordinate(Coordinate(0.5, 3.14))\n                    withCoordinate(Coordinate(5.6, 4.2))\n                    withCoordinate(Coordinate(8.8, 7.6))\n                    withCoordinate(Coordinate(4.2, 5.2))\n                    withCoordinate(Coordinate(0.5, 3.14))\n                }\n            }\n            polygon {\n                ring {\n                    withCoordinate(Coordinate(1.1, 2.2))\n                    withCoordinate(Coordinate(3.3, 4.4))\n                    withCoordinate(Coordinate(3.3, 4.6))\n                    withCoordinate(Coordinate(1.1, 2.2))\n                }\n            }\n        }\n    }\n}\n```\n```json\n{\n  \"type\": \"Feature\",\n  \"geometry\": {\n    \"type\": \"MultiPolygon\",\n    \"coordinates\": [\n      [\n        [\n          [\n            2.2,\n            1.1\n          ],\n          [\n            4.4,\n            3.3\n          ],\n          [\n            4.6,\n            3.3\n          ],\n          [\n            2.2,\n            1.1\n          ]\n        ],\n        [\n          [\n            3.14,\n            0.5\n          ],\n          [\n            4.2,\n            5.6\n          ],\n          [\n            7.6,\n            8.8\n          ],\n          [\n            5.2,\n            4.2\n          ],\n          [\n            3.14,\n            0.5\n          ]\n        ]\n      ],\n      [\n        [\n          [\n            2.2,\n            1.1\n          ],\n          [\n            4.4,\n            3.3\n          ],\n          [\n            4.6,\n            3.3\n          ],\n          [\n            2.2,\n            1.1\n          ]\n        ]\n      ]\n    ]\n  },\n  \"properties\": {}\n}\n```\n\n## License\n\n\u003e   Copyright 2019 Tobia De Koninck\n\u003e\n\u003e   Licensed under the Apache License, Version 2.0 (the \"License\");\n\u003e   you may not use this file except in compliance with the License.\n\u003e   You may obtain a copy of the License at\n\u003e\n\u003e       http://www.apache.org/licenses/LICENSE-2.0\n\u003e\n\u003e   Unless required by applicable law or agreed to in writing, software\n\u003e   distributed under the License is distributed on an \"AS IS\" BASIS,\n\u003e   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\u003e   See the License for the specific language governing permissions and\n\u003e   limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledfan%2Fkotlin-geojsondsl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fledfan%2Fkotlin-geojsondsl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fledfan%2Fkotlin-geojsondsl/lists"}