Last active
August 1, 2017 20:33
-
-
Save dshook/358a1b9a3f11c77c63f0 to your computer and use it in GitHub Desktop.
GeoJSON MapController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Net; | |
| using System.Net.Http; | |
| using System.Threading.Tasks; | |
| using System.Web; | |
| using System.Web.Http; | |
| using Models; | |
| namespace API | |
| { | |
| public class MapController | |
| { | |
| public async Task<object> Get( | |
| decimal latitude, | |
| decimal longitude, | |
| decimal NELat, | |
| decimal NELng, | |
| decimal SWLat, | |
| decimal SWLng, | |
| bool extraFilterParametersYouShouldPass | |
| ) | |
| { | |
| //fetch zip path data | |
| var geoJSONAsync = GetZipsAsync(NELat, NELng, SWLat, SWLng); | |
| //get all reporting data filtered by the inputs | |
| var occupancyListAsync = GetReportingDataAsync(extraFilterParametersYouShouldPass); | |
| await Task.WhenAll(geoJSONAsync, occupancyListAsync); | |
| var geoJSON = geoJSONAsync.Result; | |
| var occupancyList = occupancyListAsync.Result; | |
| if (occupancyList.Count == 0) | |
| { | |
| return new { }; | |
| } | |
| var zipsReturned = geoJSON.Select(x => x.zip).Distinct().ToList(); | |
| var groupedZipSales = occupancyList | |
| .GroupBy(x => x.zip) | |
| .Where(x => zipsReturned.Contains(x.Key)) | |
| .Select(x => new | |
| { | |
| zip = x.Key, | |
| rev = x.Sum(s => s.revenue), | |
| qty = x.Sum(s => s.qty) | |
| }) | |
| .ToList(); | |
| geoJSON result = new Models.geoJSON() | |
| { | |
| type = "FeatureCollection", | |
| features = geoJSON | |
| .Select(x => ZipCodeGeoJSON.selectGeoJSON(x)) | |
| .ToList() | |
| }; | |
| //fill in reporting data to geo result | |
| foreach (var groupedSale in groupedZipSales) | |
| { | |
| var g = result.features.Where(x => x.properties.zip == groupedSale.zip).FirstOrDefault(); | |
| if (g != null) | |
| { | |
| g.properties.rev = groupedSale.rev; | |
| g.properties.qty = groupedSale.qty; | |
| } | |
| } | |
| return new | |
| { | |
| summary = new | |
| { | |
| minRev = groupedZipSales.Min(s => s.rev), | |
| maxRev = groupedZipSales.Max(s => s.rev), | |
| }, | |
| geoData = result | |
| }; | |
| } | |
| public async Task<List<zipCombined>> GetZipsAsync( | |
| decimal NELat, | |
| decimal NELng, | |
| decimal SWLat, | |
| decimal SWLng | |
| ) | |
| { | |
| return await Task.Run(() => | |
| { | |
| return ZipCodeGeoJSON.GetZipsInRectangle(NELat, NELng, SWLat, SWLng); | |
| } | |
| ); | |
| } | |
| //This is all optional and specific to your need | |
| //But this is how you could fetch other reporting data you want to join in and visualize | |
| public async Task<List<ReportingClass>> GetReportingDataAsync | |
| ( | |
| bool extraParams | |
| ) | |
| { | |
| var httpContext = HttpContext.Current; | |
| return await Task.Run(() => | |
| { | |
| return ManifestMinutes.GetSessionItems(extraParams, httpContext: httpContext) | |
| .ToList(); | |
| } | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment