datagovuk / apiserver

Prototype "NII Data->API" Service
15 stars 3 forks source link

Limit results by radius around lat/lon #77

Open RamRaider opened 7 years ago

RamRaider commented 7 years ago

It appears that ALL results are returned from queries such as

//data.gov.uk/data/api/service/health/hospitals/nearest?lat=52.64105830207872&lon=1.032092571258545

which seems pointless as the returned data is much larger than it needs to be. Is there or can there be a distance limiting parameter - ie

//data.gov.uk/data/api/service/health/hospitals/nearest?lat=52.64105830207872&lon=1.032092571258545&radius=20

etc??

In the API documentation it states there is the ability to query the database directly by crafting the sql and appending to the endpoint url - but it is unclear how one would use this approach to achieve the desired distance limit mentioned.

rossjones commented 7 years ago

Yes, limiting and radius and paging are all things that need doing. There's no support for radius at present, although theoretically it's just a case of using ST_Distance_Sphere in this case it's a little more complicated.

In the meantime, so you can keep your resultsets small you could go to https://data.gov.uk/data/api/health#sql and try the following query

select * from hospitals 
order by ST_Distance(LatLong, ST_SetSRID(ST_MakePoint(52.64105830207872, 1.032092571258545), 4326))
limit 10;

If you then click download as json you should have the URL to use. Obviously it isn't as useful as radius, but it might help you progress in the meantime.

RamRaider commented 7 years ago

:) Brilliant - thanks for the useful response and code! I shall give the example sql a try and see how I get on with that approach