Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Info: GeoJSON manipulation through ORM #317

Open
PetkevichPavel opened this issue Jan 26, 2022 · 3 comments
Open

Info: GeoJSON manipulation through ORM #317

PetkevichPavel opened this issue Jan 26, 2022 · 3 comments

Comments

@PetkevichPavel
Copy link

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 PetkevichPavel changed the title GeoJSON manipulation Info: GeoJSON manipulation through ORM Jan 26, 2022
@PetkevichPavel
Copy link
Author

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

@zigzago
Copy link
Member

zigzago commented Feb 12, 2022

@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 KProperty.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

@PetkevichPavel
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants