0

I got a WCF service from which I can get a distance in meters from one point to another (latitude and lontitude) with the contract method:

public double GetDistance(double originLat, double originLng, double destLat, double destLng)

One of the points is a constant point, and the other point is one of several locations I need to extract from a database according to some other information I receive. The end goal is to get the 5 most closest locations to that constant point.

Imagine if using the WCF service cost money per request.. using the most direct approach, I would need to get all the locations from the database and then need to make a request from the service for each location.. Is there a way to somehow make it better like somehow filtering the locations in database in order to make less requests to the service?

Yonatan Nir
  • 8,303
  • 23
  • 80
  • 156
  • Are you using a WCF service to get point to point distance between 2 long/lat points? If you are then you can calculate that yourself. – Alok May 20 '19 at 12:28
  • I didn't write it, but I need an answer which doesn't involve implement the method on the client side – Yonatan Nir May 20 '19 at 12:31
  • That's a problematic constraint. Any language in existence can do this calculation. Caching won't help because the requests won't repeat much. There are just too many possible combinations of requests. I can't see any good solution that involves a client calling a remote application for the answer to a math problem. The work it does just to form the request, send it, wait for it, and parse the result is so much more complicated than the math itself. I'd be interested in why you can't do it on the client because even if it seems like you can't there might be a way. – Scott Hannen May 20 '19 at 12:36

3 Answers3

1

This method is just a mathematical function, so there's no need to host it in a WCF service. Whatever is calling this service should just have its own local version of this method. That will minimize the service requests by eliminating them, and it will be insanely faster.

From the additional details, it sounds like you're also executing a query that returns a number of points, and out of those points you want to find the five that are closest to a given location.

Caching only helps if you're making the same requests with some frequency. It's possible that the first query, which returns a collection of points, might get repeated, so it might make some sense to cache the collection of points for a given query.

But unless the location that you're comparing to those points is also frequently repeated, adding it would mess up your caching.

For example, this might benefit from caching...

Points[] GetPointsUsingSomeQuery(queryInput)

...if queryInput repeats over and over.

But if you change it to this...

Points[] GetPointsClosestToSomeLocation(queryInput, Point location)

...then any benefit of caching goes out the window if location isn't frequently repeated. You'd just be caching a bunch of data and never using it because you never make the exact same request twice.

That's also why caching probably won't help with your original function. Unless you're going to repeat exact combinations over and over, you'd never find the result you're looking for in the cache. Even if it repeats occasionally it probably isn't worth it. You'd still make a lot of requests and you'd also store lots of data you're not using in the cache.

Your best bet is to overcome whatever constraint says that you can't execute this mathematical function locally.

Scott Hannen
  • 21,450
  • 3
  • 33
  • 44
0

If you are trying to find point to point distance or flight distance between 2 long/lat points then you can look at the answer below:

SO Answer

If you are check distance by road then your only option is to cache the results between those points if it is called often. Beware with caching, your provider might forbid this and best check their T&C's.

Alok
  • 1,264
  • 11
  • 20
0

In the end, the answer is to treat the (Longitude, Latitude) as (x,y) coordinates and calculate a length of a line from the starting point to the current (x,y) with the formula:

d = sqrt((x1-x2)^2 + (y1-y2)^2)

We first read 5 points, calculating the length and keeping the max distance and the point to the max distance (with a stack or a list in order to keep several distances and points). at each point we read, we simply calculate the distance and update the distance and point if the new distance is lower

Yonatan Nir
  • 8,303
  • 23
  • 80
  • 156