![]() ![]() To set up the query, you could write the following: SELECT ST_Distance( If you already have a geom column in your table that's populated with a geography value, then you can select the value from your table without setting the projection, since it's already encoded in the geometry value. You can view the differences between the calculations in the query below: SELECT ST_Distance( Therefore, it's important that you set a boolean parameter at the end of the ST_Distance function to either true or false depending on whether your coordinates are located on a spheroid or sphere, respectively. To quickly set up a query, you could write the following, which will unfortunately give you the distance in degrees and not in meters: SELECT ST_Distance( You are given the choice to set the projection to your requirements by either manually setting the projection and providing the longitude/latitude coordinates in the query, or you can use a geometry or geography value from your geom column. ST_Distance is flexible in that you can pass in geometry or geography type object (see here for more information on these object types). To construct a basic query in SQL to find the distance between two points, use the ST_Distance function. It will allow you to define the projection you want to use, but all your measurements will return in meters. If you need to use a spheroid, or want to define another projection for your geodata, using PostGIS is a better option. ![]() The downside of calculating distances using a sphere is that it can produce an error of 0.5%, which is not ideal for every application, especially those that require a precise measurement over long distances. You cannot change the projection to one that uses a spheroid (for more information on projections, spheres and spheroids see here). The only problem with GEODIST is that it assumes your points are on a perfect sphere. You can also append to the query specific measurement units like meters, kilometers, feet, and miles.įor example, if we wanted our output in miles, we'd append mi to the command we just ran, which would give us the number of miles between Seattle and New York: GEODIST Cities Seattle "New York" mi To get the distance between Seattle and New York, we can write the following query which will give us the distance between the cities in meters: GEODIST Cities Seattle "New York"īy default, if you don't include the unit of measurement at the end of the GEODIST command, it will give you a distance in meters. Now that the coordinates are stored, we can use the GEODIST command that allows us to get the distance between any two members in a sorted set. GEOPOS Cities Seattle "New York" "San Francisco" This will give you the longitude and latitude coordinates of each city like this: GEOPOS Cities Seattle If you want to check what's stored in the Cities set, use the GETPOS command with the set name and the city name(s) you want. Redis will store your geodata and automatically set up the geospatial index for us. To do this, we will use the GEOADD command like: GEOADD Cities -122.3321 47.6062 "Seattle" Let's create a set of Cities and add three cities (Seattle, New York, and San Francisco) with their longitude and latitude coordinates. Redis and PostGIS come with commands, GEODIST and ST_Distance, that allow us do this easily, but there are reasons why choosing one database over the other may be a better choice for your use case. The most common reason why we use maps is to calculate the distance from one point to another. In this article, we discuss getting the distance between two points on a map and how Redis and the PostgreSQL extension PostGIS approach this problem. GeoFile is a series dedicated to looking at geographical data, its features and uses. ![]() GeoFile: Getting the distance using Redis and PostgreSQL postgis redis geofile Free 30 Day Trial
0 Comments
Leave a Reply. |