I'm writing an application that needs to make use of geo-referenced data, and I'd like to use MongoDB + Morphia. The application is in Scala, but if a portion needs to be Java, that's ok (yay compatibility!)
I have a class to represent the Latitude and Longitude of events:
class LatLon
{
@BeanProperty
var latDegrees : Double
@BeanProperty
var lonDegrees : Double
}
It's not a very exciting class, but it is useful in this context.
Now, I have an event that I record at a location:
class ObservedEvent
{
@BeanProperty
var observation : String = _
@Beanproperty
var location : LatLon = _
}
Now, I have a ton of observed events and I want to store them in MongoDB with Morphia. The 'location' should be stored as a GeoJson Point so I can index the collection, etc. I have tried making SimpleValueConverter's, adapters, and a few other hacks, but I haven't been able to figure out how to make this work. It seems like such a common use-case that it would be built in. Hopefully the answer here is "It's built in! Look [here]". If it is, I haven't found it :(
Thanks!
Geojson format is not strictly required; an index is possible on any key in mongo, so you can simply index latDegrees and lonDegrees keys. Plus geojson format is verbose and will increase your db size. We have been doing that for about 1 year and its caused no problems with mapping.
Yes, you do need Geojson if you wish to use the variety of nifty $near, $within geo-spatial queries or $geoNear aggregation.. But if you dont plan on using them, then dont bother imho.
When and if you need to convert your geo data from your latDegrees, lonDegrees format to the expected geojson format run this shell script. This is best as a one-time operation.
BE CAREFUL. The above script sets the geojson and unset your old format in one decisive operation. You can always create a new collection by replacing the update with insert --- on a new collection as such. The following will retain your data and make a new collection in the same DB.
Run the scrip on the commandline using the mongo shell.
I cannot help you with the core problem, nevertheless, I would suggest focusing on a BSON handler on the Reactive or Rogue Mongo driver -- opposed to extending your private code. Its cleaner and then others can use your work. Yes is a common issue and should be in the Mongo drivers.
ie. Looking at this Scala sample you see the use or BSONDocument. One could create a BSONLatLonDocument that serializes/marshalls a 2 float array into the needed geojson format.