kmongo icon indicating copy to clipboard operation
kmongo copied to clipboard

Info: GeoJSON manipulation through ORM

Open PetkevichPavel opened this issue 3 years ago • 3 comments

Hello, this is more a question than an issue, unfortunately, but I wasn't able to find how to properly work with GeoJSON through KMongo in your docs.

I will start from the DB setup:

The structure on mongodb(Atlas): db-> geo-db collection-> features

example of GeoJSON (Import with "Insert to Collection" as a JSON):

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "scalerank": 1
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              17.101984897538898,
              48.81696889911711
            ],
            [
              16.960288120194576,
              48.5969823268506
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "scalerank": 1
      },
      "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
          [
            [
              [
                12.69000613775563,
                55.609990953180784
              ],
              [
                12.089991082414741,
                54.80001455343793
              ]
            ]
          ],
          [
            [
              [
                10.912181837618363,
                56.458621324277914
              ],
              [
                10.667803989309988,
                56.08138336854722
              ]
            ]
          ]
        ]
      }
    }
  ]
}

I tried to create my own structure in order to getCollection<T>().find() it:


object GeoJson {
    @JsonIgnoreProperties(ignoreUnknown = true)
    data class Features(
        val features: List<Feature>
    )

    @JsonIgnoreProperties(ignoreUnknown = true)
    data class Feature(
        @JsonProperty("properties")
        val property: Property,
        val geometry: Geometry
    ) {
        val LatLng.asCountryGeo
            get() = CountryGeo(lat, lng, property.asAddress)

        data class Property(
            @JsonProperty("sovereignt")
            val countryName: String,
            @JsonProperty("iso_a2")
            val countryCode: String
        ) {
            val asAddress
                get() = Address(countryName, countryCode)
        }

        data class Geometry(
            val coordinates: List<Any>
        )
    }
}

Nevertheless, I wasn't able to find a way to lookup/search through ORM...

Now the questions:

  1. Do you support GeoJSON (deserialization native through ORM)?
  2. How can I lookup/search/filter/find with coordinates through ORM?

Thank you for any help 😉

PetkevichPavel avatar Jan 26 '22 23:01 PetkevichPavel

Hello @zigzago, could you provide relevant feedback or connect me with someone who will be able :) Thank you in advance.

PetkevichPavel avatar Jan 27 '22 15:01 PetkevichPavel

@PetkevichPavel you can take a look at java driver documentation https://docs.mongodb.com/drivers/java/sync/v4.3/fundamentals/crud/read-operations/geo/

For queries, KMongo supports geojson extensions (like fun <T> KProperty<T>.near(geometry: Point, maxDistance: Double? = null, minDistance: Double? = null): Bson = )

For serialization/deserialization, you have two choices:

  • use kmongo-native: Geojson serialization/deserialization is already supported under the hood by the mongo driver
  • if you prefer to use kmongo (jackson mapping) ou kmongo-serialization (kotlinx-serialization mapping), you have to create your own Jackson or kotlinx-serialization serializers for GeoJson classes - a PR is welcome

HTH

zigzago avatar Feb 12 '22 22:02 zigzago

Hi @zigzago, thank you for your response and references, I will take a look later this week, maybe your solutions will be directly what I'm trying to figure out.

Thank you one more time. Have a nice day!

PetkevichPavel avatar Feb 13 '22 18:02 PetkevichPavel