Тёмный

Geolocations Using PostGIS 

PG Casts by Hashrocket
Подписаться 1,7 тыс.
Просмотров 18 тыс.
50% 1

This PG Casts episode is sponsored by Hashrocket, a consultancy specializing in PostgreSQL; learn more at hashrocket.com. To see more PG Casts videos, visit our RU-vid channel or www.pgcasts.com
Transcript:
Hey everyone, today we're going to look at how to use the PostGIS extension to work with geolocations in Postgres.
PostGIS is a third party extension for Postgres, and can be added by using the Postgres "create extension" command.
When working with geolocations, where you're typically dealing with distance measurements, PostGIS recommends using the "geography" column type. This type is slower than the "geometry" type and has fewer functions available, but it doesn't require us to have knowledge of projections and planar coordinate systems.
Let's add a geography column to a table and see how we can work with the data.
In this example, we have a table called addresses, with a few existing entries.
We're going to use the alter table command to add a column name geolocation of type geography. The geography type can receive up to two arguments.
The first argument is an optional type modifier, which can be used to restrict the kinds of shapes and dimensions allowed for the column. Since we are going to be using latitude and longitude coordinates, we can pass point as our type modifier.
The second argument is an optional spatial resource identifier, or SRID. If the SRID option is omitted, the geography column will default to a value of 4326, which is the SRID for WGS 84, the World Geodetic System of 1984, and the standard for the Global Positioning System.
By inspecting our table again, we can see the new geography column added, with 4326 as the SRID option.
With our new geography column in place, we now need to start adding data. There are a few ways we can do this.
The first is to use strings, so we'll update addresses and set geolocation to a string in a specific format, which PostGIS refers to as a Well-Known Text representation, or WKT. In this case, our WKT starts with the type of data we're inserting, which is point, followed by parentheses and the two coordinate values. PostGIS points follow the same rules as Postgres points when it comes to latitude and longitude coordinates; they both expect longitude first, followed by latitude.
We can now read from our table, looking for the row we just tried to update, and see that there is a value for the geolocation.
The second way to update our geolocation column is to use the PostGIS ST_MakePoint function. This function accepts as arguments our longitude and latitude coordinates, again with longitude first, and saves them as a point in the database.
A thing to note on the ST_MakePoint function is that it doesn't automatically know the SRID we're looking for. In our last example, because we're inserting into the database and the column has the SRID set, we're covered.
However, if we were trying to compare or select an arbitrary point value with ST_MakePoint, the SRID would be unknown.
In such cases, we would need to wrap our ST_MakePoint function up in an ST_SetSRID function, and explicitly tell PostGIS the SRID we're intending to use.
With our geolocation column populated, we can now start comparing the distances between our addresses.
We're going to look at two distance queries. The first is determining the distance between addresses. The second is finding addresses within a certain radius of another address.
Let's start with calculating the distance between two addresses.
To calculate the distance between the Hashrocket Jacksonville office and all of the other addresses in our table, we're going to select from addresses and do a lateral join to get the Hashrocket Jacksonville address as a location to compare the other locations to. With that set up, we can complete our select statement to determine the distance between the Hashrocket Jacksonville geolocation and our other geolocations, using the "ST_Distance" function provided to us by the PostGIS module. This function expects to be given two points to compare, and returns the distance in meters between the two points.
We can see from our output that we now have the distance of each address from the Hashrocket Jacksonville office.
To only find addresses within a certain distance of the Jacksonville office, we can make a quick modification to our existing query. Since we already know how to get the distance between our two points, we can reuse the same logic passing it now as part of our where clause, and using it in an inequality to check that the distance between the addresses is less than a specific value, say 1 kilometer, or 1000 meters.
With that change, we can see from our output that we are now only including addresses that are up to a kilometer from our Hashrocket Jacksonville address.

Опубликовано:

 

7 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 26   
@cubosoft
@cubosoft 2 года назад
select a.name, ST_Distance(a.geolocation, b.geolocation) as distance from addresses a left join addresses b on (a.id b.id) where b.name = 'Hashrocket JAX' --and ST_Distance(a.geolocation, b.geolocation) < 1000 order by distance;
@_nikolanenadovic
@_nikolanenadovic 6 месяцев назад
Great job done for 5 min!
@amrqura
@amrqura 2 года назад
Love your explanation, easy to follow and very nice.
@HT61867
@HT61867 3 года назад
Amazing video
@michaelkenny4329
@michaelkenny4329 3 года назад
Wow! Really great video and super easy to follow! This is great!!
@practical-skills-school
@practical-skills-school 2 года назад
Thank you!
@williantavares4184
@williantavares4184 4 года назад
Oh my god, thank you so much!
@ameyjain3462
@ameyjain3462 3 года назад
ST_DISTANCE will return distance in mts regardless of the SRID of the geometries? I think with 4326 it will return distance in degrees right?
@raynip125
@raynip125 3 года назад
Very good video! Thank you!
@nc2809
@nc2809 3 года назад
So helpful !
@anikettiwari6885
@anikettiwari6885 3 года назад
Can someone explain what is the use of SRID?
@veggiekeller
@veggiekeller 4 года назад
Also I was wondering, why do you have to say ST_Distance(a.geolocation, myrtle_parkway.geolocation) < 1000, instead of distance < 1000. I know it doesn't work, but why is that?
@melikeyldz962
@melikeyldz962 3 года назад
hi can u help with st_within function,how can I use this ?
@dianaramirez3508
@dianaramirez3508 3 года назад
Hi, I have a temperature dataset in 2 files (.cvd and .xlsx) and I would like to create a geo-referenced map with multiple layers according to the temperature at different time points. I would like to be able to filter by layer and also select one or more combinations. Is this possible? Any advice is appreciated. Thanks in advance for your help!
@veggiekeller
@veggiekeller 4 года назад
This is a really great tutorial, but what you type is hidden unless I put the video on full screen, which makes it difficult to follow along to.
@mandata143
@mandata143 8 месяцев назад
where have I been?
@GIS-Engineer
@GIS-Engineer 4 года назад
ST_makepoint doesn't exist error Plz give solution
@veggiekeller
@veggiekeller 4 года назад
Try this. It worked for me! ST_MakePoint(longitude::double precision, latitude::double precision);
@GIS-Engineer
@GIS-Engineer 4 года назад
@@veggiekeller thanx a lot
@veggiekeller
@veggiekeller 4 года назад
@@GIS-Engineer No problem! Did it work ok?
@GIS-Engineer
@GIS-Engineer 4 года назад
@@veggiekeller plz give me ur email..if u dont have prob. I need ur help in postgresql some other question
@veggiekeller
@veggiekeller 4 года назад
@@GIS-Engineer Sure. veggiekeller@gmail.com. Im learning too, so I might not know everything, but feel free to shoot me an email. I'll answer what I can!
@undercrackers56
@undercrackers56 3 года назад
I am sure that this is a good tutorial, but I am English (from England) and cannot understand some of the things being said. (Eg: 4:04).
@polentaibife
@polentaibife Год назад
when i update the geolocation column with ST_MakePoint it only saves the lat long like this "POINT (-49.1162121 -26.9281947)" it doesnt save as it is shown on the video. Anyone else had this problem too?
@pradeepnooney3056
@pradeepnooney3056 3 года назад
Thank you !!
Далее
FOSS4G - Solving Spatial Problems with PostGIS
24:51
2019: OSM data processing with PostgreSQL/PostGIS
55:31
Solving one of PostgreSQL's biggest weaknesses.
17:12
Просмотров 193 тыс.
I've been using Redis wrong this whole time...
20:53
Просмотров 356 тыс.
Three Spatial Database Designs for PostGIS and QGIS
1:06:55
Geolocations Using Earthdistance and Cubes
3:41
Просмотров 1,4 тыс.
Daniel Silk: 10 Things I've Learnt About PostGIS
13:58
Using QGIS with PostGIS
46:34
Просмотров 36 тыс.
How do we add LIDAR to a ROS robot?
22:45
Просмотров 143 тыс.
Introduction to PostGIS
40:56
Просмотров 58 тыс.