27
Tudor Pavel 1 / 27

Dr. PostGIS or: How I Learned to Stop Worrying and Love the Docs

  • Upload
    rupicon

  • View
    51

  • Download
    0

Embed Size (px)

Citation preview

Tudor Pavel

1 / 27

Requirement:

Suggest parking areas close touser's location.

2 / 27

3 / 27

4 / 27

Calculating distances betweenpoints ain't that easy.

5 / 27

Of course, for small cities...

we can ignore the fact the planet is sphericaland see it as flat.

6 / 27

But that's still a problem unless weproperly convert from degrees to

meters/kilometers.

7 / 27

So we used this table...

8 / 27

And this table...

9 / 27

To implement a distance converter module

module DistanceConverter # ...

# Convert Latitude distance from Kilometers to Degrees def to_deg_lat(km:, lat: 45) km / ONE_LAT_DEGREE[deg_approx_lat(lat)] end

# Convert Latitude distance from Degrees to Kilometers def to_km_lat(deg:, lat: 45) deg * ONE_LAT_DEGREE[deg_approx_lat(lat)] end

# Convert Longitude distance from Kilometers to Degrees def to_deg_lng(km:, lat: 45) km / ONE_LNG_DEGREE[deg_approx_lat(lat)] end

# Convert Longitude distance from Degrees to Kilometers def to_km_lng(deg:, lat: 45) deg * ONE_LNG_DEGREE[deg_approx_lat(lat)] end

private

# ...end

10 / 27

All is well, except...

This converter is Ruby. What if we need tocalculate distances in an SQL query?

11 / 27

A naive versionSELECT (sqrt((43.1234 - latitude)̂2 + (23.1234 - longitude)̂2)) AS distFROM areas;

That's just the linear distance between two points in a plane, so it's correctwith our assumtion when in a small city?But the problem is it is using degrees which is a bigger problem the closerwe are to the poles.One longitude degree will be worth less kilometers than a latitude degree.So, what now?

12 / 27

"Here I come to save the day!"PostGIS

13 / 27

14 / 27

15 / 27

This is a driving distance 5.7 km.

16 / 27

PostGIS version of the querySELECT ST_Distance_Sphere( ST_MakePoint(43.1234, 23.1234), ST_MakePoint(latitude, longitude)) AS distFROM areas;

For the example, the straight line distance with our Ruby converter: 4.5 kmPostGIS result: 6.5 kmWat

17 / 27

Mister StackOverflow, please help

18 / 27

After some research involving...

19 / 27

20 / 27

Being reminded about projectiondistortion

https://gmaps-samples.googlecode.com/svn/trunk/poly/puzzledrag.html

21 / 27

Even starting to doubt the Earth isactually a spheroid.

Why would they have more coordinatesystems??

22 / 27

After a while...

23 / 27

I looked closer at the docs

24 / 27

Did you miss it?

25 / 27

26 / 27

ConclusionsRead the docs.

Think about the simplest possibilities first.

27 / 27