Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radius search database function #13

Open
nealf opened this issue Mar 31, 2017 · 5 comments
Open

Radius search database function #13

nealf opened this issue Mar 31, 2017 · 5 comments
Labels

Comments

@nealf
Copy link
Member

nealf commented Mar 31, 2017

Figure out the best way to implement a radius search (e.g. parks within 5 miles) through the PostgREST API. Will probably require using a function. Ideally also needs to allow for filtering based on amenities.

@nealf
Copy link
Member Author

nealf commented Apr 27, 2017

Looks like the best way to do this is going to be through a function/stored procedure. PostgREST requires named variables in functions in order to work properly, so have to make sure we do that. I think we'll probably want to pass lat, lng, and radius in miles (0 defaults to no limit/return everything).

The SELECT statement in the function will probably just use the PostGIS function ST_Distance. We'll want to return the results as geojson, and I think I've got a function somewhere that turns all of the row results into a single json document...I'll try to track that down.

@nealf nealf changed the title Radius search via the API Radius search database function Apr 27, 2017
@nealf
Copy link
Member Author

nealf commented Apr 27, 2017

Now that I think about it, if our function just returns regular rows, I think PostgREST will convert them to json, though I haven't confirmed that for functions. So let's try that first, because the alternative is trying to modify the following query that takes the json rows and creates one big json document :-)

SELECT row_to_json(featcoll) FROM 
            (SELECT 'FeatureCollection' As type, array_to_json(array_agg(feat)) As features FROM 
                (SELECT 'Feature' As type, ST_AsGeoJSON(geom)::json As geometry,
                    row_to_json((SELECT l FROM (
			SELECT countyfp AS fips, name
		     ) As l)) As properties 
                    FROM geo.us_counties AS g
                    WHERE statefp = '51')
                AS feat)  
            AS featcoll;

@nealf nealf added the db label May 25, 2017
@nealf
Copy link
Member Author

nealf commented May 26, 2017

Here's a tutorial that might be helpful in returning a table from the function: http://www.postgresqltutorial.com/plpgsql-function-returns-a-table/

@nealf
Copy link
Member Author

nealf commented Jun 22, 2017

Here's an example javascript request to our new endpoint:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "https://parks.api.codefornrv.org/rpc/get_radius_miles",
  "method": "POST",
  "headers": {
    "accept": "application/json",
    "content-type": "application/x-www-form-urlencoded",
    "cache-control": "no-cache",
    "postman-token": "17d1e514-8278-2df4-0c80-d201ec4963ee"
  },
  "data": {
    "latitude": "37.2341",
    "longitude": "-80.4139",
    "radius_size": "1"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

@jhawley
Copy link
Contributor

jhawley commented Jun 22, 2017

That works, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants