public ActionResult Search()
 {
     using (var context = new SpatialDemoContext())
     {
         dynamic viewModel = new ExpandoObject();
         viewModel.MaxDistances = GetDistanceSelectList(5000);
         viewModel.PostalCode = string.Empty;
         viewModel.NeedsInitialization = GetNeedsInitialization(context);
         viewModel.SearchFormTitle = "Search Places by Postal Code";
         return View(viewModel);
     }
 }
        public ActionResult List(string postalCode, int maxDistance)
        {
            using (var context = new SpatialDemoContext())
            {
                dynamic viewModel = new ExpandoObject();

                // set up core viewmodel stuff for the view
                viewModel.NeedsInitialization =
                viewModel.PostalCode = postalCode;
                viewModel.MaxDistance = maxDistance;
                viewModel.MaxDistances = GetDistanceSelectList(maxDistance);
                viewModel.NeedsInitialization = GetNeedsInitialization(context);
                viewModel.SearchFormTitle = "Search Again";

                // get the origin postal code from the database.
                // we need this to get the lat/long/location of the origin postal code.
                var startPoint = context.PostalCodes
                    .Where(p => p.Code == postalCode)
                    .FirstOrDefault();

                // if no origin postal code was found, bail
                if (startPoint == null)
                {
                    viewModel.NoZipCode = true;
                }
                else
                {
                    viewModel.NoZipCode = false;

                    var meters = maxDistance * MetersToMiles;

                    // do the search for places based on the origin's lat/long location,
                    // and select into a new viewmodel object ordered by distance
                    var results = context.Places
                        .Where(p => p.Location.Distance(startPoint.Location) <= meters)
                        .Select(p => new PlaceResult()
                        {
                            Name = p.Name,
                            Distance = (p.Location.Distance(startPoint.Location) / MetersToMiles).Value,
                            Latitude = p.Latitude,
                            Longitude = p.Longitude
                        })
                        .OrderBy(p => p.Distance)
                        .ToList();

                    viewModel.Results = results;
                }
                return View(viewModel);
            }
        }
        public ActionResult Load()
        {
            // This is just a big parsing of two flat, tab-delimited files.
            // It's just to get data into the database. Nothing fancy, but
            // take note of the use of DbGeography and the POINT string to
            // insert spatial geography types into the database.

            using (var context = new SpatialDemoContext())
            {
                var stopwatch = new Stopwatch();
                stopwatch.Start();

                dynamic viewModel = new ExpandoObject();

                if (context.PostalCodes.FirstOrDefault() != null)
                {
                    viewModel.PostalCodesInserted = 0;
                }
                else
                {
                    // load US postal codes
                    var path = Server.MapPath(UnitedStatesDataFile);
                    var lines = System.IO.File.ReadAllLines(path);

                    foreach (var line in lines)
                    {
                        var parts = line.Split(Tab);
                        var postalCode = new PostalCode()
                        {
                            Code = parts[1],
                            Latitude = double.Parse(parts[9]),
                            Longitude = double.Parse(parts[10])
                        };

                        // Here is where we set the SQL Geography spatial field
                        // on the Postal Code.
                        postalCode.Location = DbGeography.FromText(
                            string.Format(GetSqlPointString(postalCode.Latitude, postalCode.Longitude)));
                        context.PostalCodes.Add(postalCode);
                    }

                    viewModel.PostalCodesInserted = context.SaveChanges();
                }

                if (context.Places.FirstOrDefault() != null)
                {
                    viewModel.PlacesInserted = 0;
                }
                else
                {
                    // load places
                    var path = Server.MapPath(PlacesDataFile);
                    var lines = System.IO.File.ReadAllLines(path);

                    foreach (var line in lines)
                    {
                        var parts = line.Split(Tab);
                        var place = new Place()
                        {
                            Name = parts[0],
                            Latitude = double.Parse(parts[1]),
                            Longitude = double.Parse(parts[2])
                        };

                        // set the SQL Geography spatial field on the Place.
                        place.Location = DbGeography.FromText(
                            GetSqlPointString(place.Latitude, place.Longitude));
                        context.Places.Add(place);
                    }
                    viewModel.PlacesInserted = context.SaveChanges();
                }

                stopwatch.Stop();
                viewModel.Elapsed = stopwatch.Elapsed.TotalSeconds;
                return View(viewModel);
            }
        }
 private bool GetNeedsInitialization(SpatialDemoContext context)
 {
     return context.Places.FirstOrDefault() == null
         || context.PostalCodes.FirstOrDefault() == null;
 }