Advertisement
LaughingMan

Geonames - RawPostal - EF/MVC - Get all within radius of X

Jun 1st, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.93 KB | None | 0 0
  1.  
  2.         public ActionResult Details(string countryCode, string stateCode, string countyCode, string postalCode, int radius, string featureClass, string featureCode)
  3.         {
  4.             GeonameEntities db = new GeonameEntities();
  5.  
  6.             RawPostal postal =
  7.                 db.RawPostals.FirstOrDefault(p => p.countryCode == countryCode && p.admin1code == stateCode && p.admin2code == countyCode && p.postalCode == postalCode);
  8.             if (postal == null || postal.latitude == null || postal.longitude == null)
  9.             {
  10.                 return HttpNotFound();
  11.             }
  12.  
  13.             int radiusMeters = radius * 1609;
  14.  
  15.             DbGeography posBuffer = postal.geog.Buffer(radiusMeters);
  16.             DbGeography negBuffer = postal.geog.Buffer(-radiusMeters);
  17.             DbGeography difference = posBuffer.Difference(negBuffer);
  18.  
  19.             List<RawData> results = db.RawDatas.Where(p => p.featureClass == featureClass && p.featureCode == featureCode && p.geog.Intersects(difference)).OrderBy(o => o.asciiName).ToList();
  20.  
  21.             double north = (results.Max(p => p.latitude) ?? 0); // North
  22.             double east = (results.Max(p => p.longitude) ?? 0); // East
  23.             double south = (results.Min(p => p.latitude) ?? 0); // South
  24.             double west = (results.Min(p => p.longitude) ?? 0); // West
  25.  
  26.             double latAngle = north - south; // Latitude Angle
  27.             double longAngle = east - west; // Longitude Angle
  28.  
  29.             double centerLat = north - latAngle; // Center Latitude
  30.             double centerLong = east - longAngle; // Center Longitude
  31.  
  32.             if (longAngle < 0)
  33.             {
  34.                 longAngle += 360;
  35.             }
  36.             if (latAngle < 0)
  37.             {
  38.                 latAngle += 360;
  39.             }
  40.            
  41.             DaveDetail model = new DaveDetail
  42.                 {
  43.                     Latitude = (double)postal.latitude,
  44.                     Longitude = (double)postal.longitude,
  45.                     LatitudeAngle = latAngle,
  46.                     LongitudeAngle = longAngle,
  47.                     CenterLatitude = centerLat,
  48.                     CenterLongitude = centerLong,
  49.                     North = north,
  50.                     East = east,
  51.                     South = south,
  52.                     West = west,
  53.                     Zoom = radius,
  54.                     Pins = results
  55.                 };
  56.  
  57.             return View(model);
  58.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement