How to transform a location (city-state-country) to geoJson

Hi there,

The document for using geo coordinates for Mongo queries are really very helpful!

On top of that, I am wondering usually how we could transform location, say we collect from customer in city-state-country, to the geo coordinates that could be recognized by Mongo? and vice versa.

I am new to this, and thank you for any help with this!

Hi @williamwjs ,

Usually you will need an external provider to do this translation , there are plenty of those.

The one which is super popular is google geocodes

Once you get the results from google with the coordinates in mongo with a geo index you good to go

Pavel

3 Likes

Hello @williamwjs
In MongoDB, you can store location data as GeoJSON data type, which supports various geometry types such as Point, LineString, Polygon, etc. To store a location as a Point in MongoDB, you can create a document with a field containing a GeoJSON Point object that specifies the longitude and latitude coordinates of the location.

For example, to store the location of “New York City, NY, USA” as a Point, you can create a document like this:

{
    "name": "New York City",
    "location": {
        "type": "Point",
        "coordinates": [-74.0060, 40.7128]
    }
}

To convert location in city-state-country to geo coordinates that can be recognized by MongoDB, you can use a geocoding service as I mentioned before and store the result in the same format as above.

On the other hand, to convert geo coordinates stored in MongoDB to a city, state, and country, you can use reverse geocoding as I’ve mentioned before, after that you can store the result in your desired format.

It is important to note that MongoDB also supports spatial indexes such as 2d and 2dsphere indexes, which enable efficient querying of GeoJSON data for proximity and location-based queries

1 Like

Hi @Pavel_Duchovny @Sumanta_Mukhopadhyay , thank you for your reply and suggestions!!!

A question related to this - I used Google geocoding to format “CA”, and it returned a json with geometry bounds, including only two pairs of coordinates - northeast and southwest (similar to the example json response in Geocoding request and response  |  Geocoding API  |  Google Developers). However, the mongodb Polygon needs at least four coordinate pairs, which specify the same position as the first and last coordinates. I guess the question would be whether there’s a library I could use to transform between the Google geocoding and the mongodb GeoJson?

Thank you!

Hi @williamwjs ,

Perhaps the best way is to try and assume a reasonable polygon in the middle coordinates. Why do you need a polygon and a geoNear is not enough:

# Retrieve coordinates of city from Google Geocoding API
latitude, longitude = get_coordinates_from_google(city)

# Create list of 4 points in the format [longitude, latitude]
polygon = [[longitude, latitude], [longitude-0.1, latitude-0.1], [longitude+0.1, latitude-0.1], [longitude, latitude]]

# Use $geoWithin operator in MongoDB query with $polygon operator
results = collection.find({"location": {"$geoWithin": {"$polygon": polygon}}})

Thanks
Pavel

Got it! Thank you!!!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.