![]() The markers show the points returned by the search, while the polygon generated is tessellated (follows the earth’s curvature). The following example shows the difference between the two functions when a large polygon is used. It is necessary to use degree coordinates in this case (as opposed to GEODIST() which uses radians), otherwise GEOPOLY2D will act the same as POLY2D(). Instead, GEOPOLY2D() creates polygons that take the Earth’s curvature into consideration. For this reason, POLY2D() is accurate for polygons which have sides shorter than 500km. Although the world map is in general represented on a plain map, geographic coordinates are not a plain system because of the earth’s curvature. POLY2D() is a function that creates a polygon in a cartesian system. For this, the CONTAINS() function can determine if a coordinate belongs to a polygon area created with POLY2D() or GEOPOLy2D() function. In some cases, you might want to test if a coordinate belongs to an area. Searching around a coordinate is not the only type of geo-search that can be made. The resulting distance is in miles, but it can be easily converted to meters by doing distance_miles*1609. In future versions we plan introducing a new geo distance function that employs this algorithm. ![]() This model works with degrees as coordinates ( replace input_latitude and input_longitude with the desired geo anchor). ![]() Mysql > SELECT *, SQRT ( 69.1 * 69.1 * (latitude_deg - input_latitude ) * (latitude_deg - input_latitude ) + 53 * 53 * (longitude_deg - input_longitude ) * (longitude_deg - input_longitude ) ) as distance_miles FROM geodemo WHERE distance_miles < 6.25 LIMIT 0, 100 A good alternative is the Polar flat-Earth algorithm, which can be written as an expression in Sphinx. In many cases, being precise down to the meter is not necessary, and other faster algorithms should be used. The current GEODIST() function uses the haversine algorithm to calculate distance, which is precise, but intensive. For this, we can safely assume that we can add fulltext search on state code, since it shouldn’t affect the result (however our source data seems to have 3 records without state_code, this is why you see a small difference in total_found). The query does a 10km search inside San Francisco. The improvement of using fulltext filtering can easily be seen using our code sample: Another common way is to simply group areas in grids and tag each grid with a label that is inserted in a fulltext search. In general, this is a logical step, since real locations are always part of administrative divisions, like counties, cities, and states. For this reason, it’s good to do a fulltext search to trim down the number of documents for which geo-distance is calculated. If the search does fulltext matching, all other WHERE clauses and expressions declared in the SELECT statement are calculated over the result set provided by the fulltext match. In fullscan searches (no fulltext matching) it assumes calculating a geo-distance for all documents in the index. Here, we do a radius search around point (0.659298124, -2.136602399) in the limit of 10000 meters (10km).Ĭalculating geo-distances is a somewhat expensive operation. Mysql > SELECT *, GEODIST ( 0.659298124, - 2.136602399, latitude, longitude ) as distance FROM geodemo WHERE distance < 10000 ORDER BY distance ASC LIMIT 0, 100 At output, GEODIST() offer the distance in meters between the two points. For example, MySQL has RADIANS() and DEGREES() functions and PHP has deg2rad() and rad2deg(). Most databases and programming languages offer functions to convert from degrees to radians and vice-versa. In general, geo-coordinates are stored as degrees, so you need to convert them at both indexing time and at searching time. At input, the GEODIST() function expects 2 pairs of coordinates in radians – that means float type attributes in Sphinx. Sphinx is able to provide this through the SphinxQL function GEODIST() and it’s API counterpart SetGeoAnchor() which calculates distances between 2 coordinates, taking the earth’s curvature into account. If you need to provide this kind of search, Sphinx has you covered. They may be expecting to see results within a certain distance from their current location, or they may want to limit their search to a specific region (independent of their current location). When users come to your site and search for, lets say real estate, they will most likely be expecting to have some control over the region in which they are searching. Geo-distance search is a necessary component of any search solution.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |