Wpis z mikrobloga

#prograowanie #mapy #sql #mysql #bazydanych

Męczę się z pewnym problemem od jakiegoś czasu i mam chyba blokadę. Dlatego zwracam się do Was z prośbą o pomoc.

Mianowicie chcę wyciągnąć z bazy obszary leżące w danym obszarze. Obszary zdefiniowane są za pomocą dwóch lokalizacji wyrażonych za pomocą koordynatów: south-west i north-east. Zapytanie też jest złożone z wartości south-west i north-east.

W załączonej grafice opisane jest mniej więcej to o co mi chodzi. Zapytanie powinno zwrócić obszary: zielony, czerwony, niebieski. Powinno pominąć żółty.

Jak wyrazić takie zapytanie w SQL?

Przykładowe parametry:
northEastLongitude=0.035306
northEastLatitude=51.540547
southWestLongitude=-0.077096
southWestLatitude=51.477803
siemanko - #prograowanie #mapy #sql #mysql #bazydanych

Męczę się z pewnym probleme...

źródło: comment_J8n6dGoNeehUy6OSsF4YPgvhal4NwYMV.jpg

Pobierz
  • 4
@siemanko:
łopatologicznie sklejając wyszłoby coś takiego:

where point is within green and point is within red and point is within blue and not point is within yellow
gdzie każde '... is within ...' to ifologia w stylu lat>=minLat and lat<=minLat and lng>=minLng and lng<=maxLng (pewnie nie będziesz tego kleić ręcznie, więc może jakiś kod to przygotowywać).
będzie cholernie mało czytelne ale będzie działać, a kiedyś można zrefaktorować do czegoś ładniejszego ;)
@ppawel: Dzięki za pomoc tylko ja nie szukam punktu ale obszaru :)
Jak na razie mam coś takiego generującego zapytanie w Hibernate i wygląda ok, choć nie zrobiłem jeszcze pełnych testów:

areaCriteria.add(Restrictions.or(
Restrictions.and(
Restrictions.between("area.southWest.latitude", area.getSouthWest().getLatitude(), area.getNorthEast().getLatitude()),
Restrictions.between("area.southWest.longitude", area.getSouthWest().getLongitude(), area.getNorthEast().getLongitude())
),
Restrictions.and(
Restrictions.between("area.northEast.latitude", area.getSouthWest().getLatitude(), area.getNorthEast().getLatitude()),
Restrictions.between("area.northEast.longitude", area.getSouthWest().getLongitude(), area.getNorthEast().getLongitude())
),
Restrictions.and(
Restrictions.between("area.southWest.latitude", area.getSouthWest().getLatitude(), area.getNorthEast().getLatitude()),
Restrictions.between("area.northEast.longitude", area.getSouthWest().getLongitude(), area.getNorthEast().getLongitude())
),
Restrictions.and(
Restrictions.between("area.northEast.latitude", area.getSouthWest().getLatitude(), area.getNorthEast().getLatitude()),
Restrictions.between("area.southWest.longitude", area.getSouthWest().getLongitude(), area.getNorthEast().getLongitude())
)
));

Nie wiem czemu ale blok kod