Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 699309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:22:20+00:00 2026-05-14T03:22:20+00:00

I’m storing Points Of Interest (POI) in PostgreSQL database, and retrieve them via PHP

  • 0

I’m storing Points Of Interest (POI) in PostgreSQL database, and retrieve them via PHP script to Android application. To reduce internet usage I want my mobile app to know if there are any points in the neighborhood of currently displayed area.

My idea is to store bounds of the rectangle containing all points already retrieved (in other words: nearest point on the left (West) of most west already retrieved, nearest point above (North) of most north already retrieved etc.) and I will make next query when any edge of screen goes outside of this bounds.

Currently I can retrieve points which are in “single screen” (in the area covered by currently displayed map) using:

SELECT * FROM ch WHERE loc <@ (box '((".-$latSpan.", ".$lonSpan."),(".$latSpan.", ".-$lonSpan."))' + point '".$loc."')

Now I need to know four most remote points in each direction, than I will be able to retrieve next four “more remote” points.

Is there any possibility to get those points (or box) directly from PostgreSQL (maybe using some “aggregate points to box” function)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:22:20+00:00Added an answer on May 14, 2026 at 3:22 am

    You can use the distance-to operator (<->) combined with the MIN aggregate function to find the closest distance, thus reducing the problem to finding the other columns for the row with some minimal quantity. The strictly left of/right of/above/below operators (<<, >>, |>>, <<|) can be used to limit the points to one side of the box. Since two different points might have the same distance, we’ll limit the result to 1 row. Assuming screen orientation, where coordinates increase down and to the right (rather than map orientation, which increases North and East), we get:

    -- Above, or North
    SELECT * FROM ch WHERE loc <<| screen AND (loc <-> screen) = (
      SELECT MIN(loc <-> screen) AS mindist FROM ch
        WHERE loc <<| screen
    ) LIMIT 1
    
    -- Right, or East
    SELECT * FROM ch WHERE loc >> screen AND (loc <-> screen) = (
      SELECT MIN(loc <-> screen) AS mindist FROM ch 
        WHERE loc >> screen
    ) LIMIT 1
    
    -- Below, or South
    SELECT * FROM ch WHERE loc |>> screen AND (loc <-> screen) = (
      SELECT MIN(loc <-> screen) AS mindist FROM ch
        WHERE loc |>> screen
    ) LIMIT 1
    
    -- Left, or West
    SELECT * FROM ch WHERE loc << screen AND (loc <-> screen) = (
      SELECT MIN(loc <-> screen) AS mindist FROM ch 
        WHERE loc << screen
    ) LIMIT 1
    

    Note that the closest point in a horizontal direction might also be the closest point in a vertical direction; that is, the union of the above four statements might be less than four rows.

    We can get the four closest points with:

    SELECT *, (loc <-> screen) AS distance FROM ch 
      WHERE NOT loc <@ screen
      ORDER BY distance
      LIMIT 4
    

    However, note that some of the closest points might be in the same direction as each other.

    We can get the closest point overall with

    SELECT *, (loc <-> screen) AS distance FROM ch 
      WHERE distance = (
          SELECT MIN(loc <-> screen) AS mindist FROM ch
      )
      LIMIT 1
    

    or

    SELECT *, (loc <-> screen) AS distance FROM ch 
      WHERE NOT loc <@ screen
      ORDER BY distance
      LIMIT 1
    

    When calculating the minimum (or maximum) of a column, the first would be preferable, since the DBMS could use an index on the column (if any) and not need to scan the table. Since the distance is a calculated value, a table scan is always needed and the queries will have similar performance. A query analysis might turn up some other reason to prefer a statement, so you should do so before picking an approach.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.