コード例 #1
0
        public CrimeLocationsRequestModel CrimeLocationsRequest(int crimeLocationTypeId, double latitude, double longitude, int radius)
        {
            //Init main model
            CrimeLocationsRequestModel crimeLocationsRequestModel = new CrimeLocationsRequestModel(false);

            //----------------------------------------------------------------------------------------------------//

            //get crime location relationship from DB
            CrimeLocationTypeModel clModel = null;

            CrimeLocationType dbModel = unitOfWork.CrimeLocationTypeRepository.GetByID(crimeLocationTypeId);
            clModel = new CrimeLocationTypeModel(dbModel);

            //check if model found
            if(clModel == null || clModel.Id == 0)
            {
                crimeLocationsRequestModel.Status.setNoClRelationshipFound();
                return crimeLocationsRequestModel;
            }

            //--------------------------------------------------------------------------------------------------//

            //Create GooglePLacesAPI controller
            GooglePlacesApiController googlePlacesController = new GooglePlacesApiController();

            //Get nearby locations
            GooglePlacesModel googlePlacesModel = googlePlacesController.NearbySearch(clModel.LocationType, latitude, longitude, radius);

            //check for errors
            crimeLocationsRequestModel.Status.setGooglePlacesError(googlePlacesModel);

            if (!crimeLocationsRequestModel.Status.SafeToContinue)
            {
                return crimeLocationsRequestModel;
            }

            //--------------------------------------------------------------------------------------------------//

            //Create DataPoliceAPI controller
            DataPoliceUKApiController dataPoliceUkController = new DataPoliceUKApiController();

            for(int i = 0; i <googlePlacesModel.Results.Count(); i++)
            {
                //Get crime data for each nearby location
                DataPoliceUkModel dataPoliceUkModel = dataPoliceUkController.CrimeAtLocation2(
                    googlePlacesModel.Results[i].Geometry.Location.Latitude,
                    googlePlacesModel.Results[i].Geometry.Location.Longitude,
                    clModel.CrimeTypes,
                    DateTime.Now.Month,
                    DateTime.Now.Year
                );

                //check result status before continuing
                foreach(var error in dataPoliceUkModel.Status.Errors)
                {
                    error.LocationRequestIndex = i;
                }
                crimeLocationsRequestModel.Status.Errors.AddRange(dataPoliceUkModel.Status.Errors);

                //bind location + crime data to new CrimeLocationModel and add to main model
                crimeLocationsRequestModel.CrimeLocations.Add(new CrimeLocationModel(googlePlacesModel.Results[i], dataPoliceUkModel));
            }

            return crimeLocationsRequestModel;
        }
コード例 #2
0
        public CrimeLocationsRequestModel CrimeLocationsRequestWithStats(int crimeLocationTypeId, double latitude, double longitude, int radius)
        {
            //Init main model
            CrimeLocationsRequestModel crimeLocationsRequestModel = new CrimeLocationsRequestModel(true);

            //----------------------------------------------------------------------------------------------------//

            //get crime location relationship from DB
            CrimeLocationTypeModel clModel = null;

            CrimeLocationType dbModel = unitOfWork.CrimeLocationTypeRepository.GetByID(crimeLocationTypeId);
            clModel = new CrimeLocationTypeModel(dbModel);

            //check if model found (cant hav Id zero either - stupid ef seed migration identity problem)
            if (clModel == null || clModel.Id == 0)
            {
                crimeLocationsRequestModel.Status.setNoClRelationshipFound();
                crimeLocationsRequestModel.Stats.EvaluateRequest(crimeLocationsRequestModel.CrimeLocations);
                return crimeLocationsRequestModel;
            }

            //--------------------------------------------------------------------------------------------------//

            //Create GooglePLacesAPI controller
            GooglePlacesApiController googlePlacesController = new GooglePlacesApiController();

            //Get nearby locations
            crimeLocationsRequestModel.Stats.locationRequestStopwatch.Start();
            GooglePlacesModel googlePlacesModel = googlePlacesController.NearbySearch(clModel.LocationType, latitude, longitude, radius);
            crimeLocationsRequestModel.Stats.locationRequestStopwatch.Stop();
            crimeLocationsRequestModel.Stats.LocationRequestTime = crimeLocationsRequestModel.Stats.locationRequestStopwatch.ElapsedMilliseconds;

            //check for errors
            crimeLocationsRequestModel.Status.setGooglePlacesError(googlePlacesModel);

            if (!crimeLocationsRequestModel.Status.SafeToContinue)
            {
                crimeLocationsRequestModel.Stats.EvaluateRequest(crimeLocationsRequestModel.CrimeLocations);
                return crimeLocationsRequestModel;
            }

            //--------------------------------------------------------------------------------------------------//

            //Create DataPoliceAPI controller
            DataPoliceUKApiController dataPoliceUkController = new DataPoliceUKApiController();

            for (int i = 0; i < googlePlacesModel.Results.Count(); i++)
            {
                //Get crime data for each nearby location
                crimeLocationsRequestModel.Stats.crimeRequestStopwatch.Start();
                DataPoliceUkModel dataPoliceUkModel = dataPoliceUkController.CrimeAtLocation2(
                    googlePlacesModel.Results[i].Geometry.Location.Latitude,
                    googlePlacesModel.Results[i].Geometry.Location.Longitude,
                    clModel.CrimeTypes,
                    DateTime.Now.Month,
                    DateTime.Now.Year
                );
                crimeLocationsRequestModel.Stats.crimeRequestStopwatch.Stop();
                crimeLocationsRequestModel.Stats.CrimeRequestTimes.Add(crimeLocationsRequestModel.Stats.crimeRequestStopwatch.ElapsedMilliseconds);
                crimeLocationsRequestModel.Stats.crimeRequestStopwatch.Reset();

                //check result status before continuing
                foreach (var error in dataPoliceUkModel.Status.Errors)
                {
                    error.LocationRequestIndex = i;
                }
                crimeLocationsRequestModel.Status.Errors.AddRange(dataPoliceUkModel.Status.Errors);

                //bind location + crime data to new CrimeLocationModel and add to main model
                crimeLocationsRequestModel.CrimeLocations.Add(new CrimeLocationModel(googlePlacesModel.Results[i], dataPoliceUkModel));
            }

            crimeLocationsRequestModel.Stats.EvaluateRequest(crimeLocationsRequestModel.CrimeLocations);
            return crimeLocationsRequestModel;
        }