开发者

How to represent geographical locations

I make the user interface appear adding integration with facebook and enabling combining a search with a category and a location:

How to represent geographical locations

This poses some problems to me not only with the facebook integration but also with locations. I don't fetch the locations from the datastore, I just put them in plain html:

<select name="w" id="searcharea">    
<option value="1" >Entire India</option>            
<option value="2" > Andaman & Nicobar Islands</option>          
<option value="3" > Andhra Pradesh</option>

etc. We see that the option values make no sense: 1,2,3 has no meaning and I just put the numbers there. The records in the datastore can match based on longitude+latitude+radius so for instance an URL like ?lat=-22.45&lon=-43.12&r=294200 is a parametrization that match开发者_StackOverflow中文版es Rio de Janeiro with surroundings where the radius is a bit arbitrarily chosen in this case to match Rio de Janeiro and not Sao Paulo. Rounding off the longitudes and latitudes to 2 decimals is also decided as "good enough". But how do I map between the HTML and the datastore? An HTML option value has only one value and longitude + latitude + radius are three values. I'd like to find a solution that covers everything without needing to add my own geography codes. Longitude, latitude and radius are international standard so naturally I wish to avoid creating my own IDs for locations. I probably could make an algorithm that the value of the option value is one number that decodes to three values for instance the option value 123456789876 would decode to longitude 1234, latitude 5678 and radius 9876. Can you recommend any solution and/or tell me other advantages and disadvantages the possible solutions have? If you want to inspect the website I talk about, you can via this link koolbusiness.com

I'd like to stay "compatible" with other solutions such as geomodel and google maps so also allowing user to select an "administrative area", a "city", a "country" in a way compatible with google maps since they already make and update that structure and its contens while I've used the python library geomodel to enable a geography for my entities:

class A(GeoModel,search.SearchableModel):      
    primary_image=blobstore.BlobReferenceProperty() 
    postaladress=db.PostalAddressProperty(indexed=False,verbose_name="postaladdress")
    ...

Implementing a basic search with geomodel works:

m=int(self.request.get('r')) if self.request.get('r') else 804670#radius
lat = self.request.get('lat')
lon = self.request.get('lon')
articles = A.proximity_fetch(A.all(),db.GeoPt(lat, lon),max_results=PAGESIZE+1, max_distance=m)
articles = sorted(articles, key=lambda x: x.modified, reverse=True)

But how integrate the view with the backend since the option value only admit a single value? Should I encode 3 values (lon,lat,radius) encoded as a single value? Or some other solution? Thank you


Why not just do:

<option value="{lat:-22.45,long:-43.12,r:294200}">Rio De Janeiro</option>

Then you can jsondecode the value to get the components of it.


Most likely you look for a quadkey. A quadkey is a spatial index or a space-filling-curce. It looks like a quadtree or a fractal and reduce the 2d complexity to a 1d complexity. Google-Maps and Bing-Maps are using a z-curve because it's easy to solve. A hilbert-curve is better at space filling but more difficult to solve. The idea of a quadkey is to search for a tile and its locations from left to right. For example if your quadkey is 12333222244 you can search for 1233* and you get all locations from that tile and after or below that tile because it's a tree data structure.


The easiest way to do this would be to define a lookup table in a configuration file in your code. For instance:

location_map = {
  1: {'name': Rio De Janeiro', 'lat': -22.45, 'long': -43.12, 'radius': 294200},
  # ...
}

Then, you can simply use the option value as the key you look up the values in the array by. You can also use this array to generate the form field by iterating over it and outputting name/value pairs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜