def __init__(self,northLattitude,westLongitude,southLattitude,eastLongitude): self.NorthLattitude = float(northLattitude) self.WestLongitude = float(westLongitude) self.SouthLattitude = float(southLattitude) self.EastLongitude = float(eastLongitude) self.NW_GpPoint = GpPoint(self.NorthLattitude,self.WestLongitude) self.SE_GpPoint = GpPoint( self.SouthLattitude,self.EastLongitude)
def CreateLocationByProximityQuery(self): """Creates a query for locations within random miles from random center location. Returns a dictionary of search query parameters. """ self.CenterPoint = self.CreateRandomLattitudeLongitude() self.SearchDistanceMiles = random.uniform(self.minDistanceMiles, self.maxDistanceMiles) self.SearchDistanceMeters = GpMath.milesToMeters( self.SearchDistanceMiles) QueryParameterDic = {} QueryParameterDic['SearchType'] = 'LocationByProximity' QueryParameterDic['MaxResults'] = self.maxSearchResults QueryParameterDic['MaxDistanceMeters'] = self.SearchDistanceMeters QueryParameterDic['CenterPt'] = self.CenterPoint QueryParameterDic['HasShopsOnly'] = 'True' QueryParameterDic['FilterByTrueDistance'] = 'True' # Get a list of GpGridCells to search gpSearch = GpSearch() gpSearch.MaxSearchCellCount = self.MaxSearchCellCount centerGpPoint = GpPoint(self.CenterPoint[0], self.CenterPoint[1]) gpSearch.ComputeSearchListForMetersProximity(centerGpPoint, self.SearchDistanceMeters) self.FinalSearchResolution = gpSearch.FinalSearchResolution self.SearchGpGridCells = gpSearch.SearchCellList QueryParameterDic['SearchGridCells'] = self.SearchGpGridCells return QueryParameterDic
def GetLocationsOrderedByProximity(self, maxSearchResults, maxDistanceMeters, LocationAllEntityAttributeDic, centerPt): """ Make a list of (EntityKeyName,DistanceMeters,GeoCell) sorted by DistanceMeters Limit the number of list entries to maxSearchResult and limit entries by MaxDistanceMeters. EntityKeyName and location (lattitude,longitude) are extracted from the supplied LocationAllEntityAttributeDic. """ entityDistanceMeters = [] for entityKeyName in LocationAllEntityAttributeDic: attributeDic = LocationAllEntityAttributeDic[entityKeyName] longitudeStr = attributeDic['longitude'] lattitudeStr = attributeDic['lattitude'] locationPt = float(lattitudeStr), float(longitudeStr) locationGpPoint = GpPoint(locationPt[0], locationPt[1]) locationGpGridPoint = GpGridPoint() locationGpGridPoint.InitFromGpPoint(locationGpPoint, 16) locationGpCell = locationGpGridPoint.ToLongString() distanceInMeters = self.distanceMeters(locationPt, centerPt) if distanceInMeters <= maxDistanceMeters: tup = entityKeyName, distanceInMeters, locationGpCell entityDistanceMeters.append(tup) self.EntityDistanceMetersSorted = sorted(entityDistanceMeters, key=self.GetDistance) if len(self.EntityDistanceMetersSorted) > maxSearchResults: self.EntityDistanceMetersSorted = self.EntityDistanceMetersSorted[ 0:maxSearchResults]
def _GetBoundingBoxCells(self): """ Get three GpGridPoint corners of a Bounding Box for distanceMeters from a center point. The corner points are set to the MaxSearchResolution """ centerGpPoint = self.CenterGpPoint degreesOffset = GpMath.distanceMetersToDegrees(centerGpPoint, self.SearchBoundaryMeters) # Get bounding corner points swCornerF = GpPoint(centerGpPoint.lat - degreesOffset.lat, \ centerGpPoint.lon - degreesOffset.lon) self.SWCornerGridPoint = GpGridPoint() self.SWCornerGridPoint.InitFromGpPoint(swCornerF, self.MaxSearchResolution) nwCornerF = GpPoint(centerGpPoint.lat + degreesOffset.lat, \ centerGpPoint.lon - degreesOffset.lon) self.NWCornerGridPoint = GpGridPoint() self.NWCornerGridPoint.InitFromGpPoint(nwCornerF,self.MaxSearchResolution) seCornerF = GpPoint(centerGpPoint.lat - degreesOffset.lat, \ centerGpPoint.lon + degreesOffset.lon) self.SECornerGridPoint = GpGridPoint() self.SECornerGridPoint.InitFromGpPoint(seCornerF,self.MaxSearchResolution)
def RunLocationByProximityQuery(self): """Runs a query for locations within random miles from random center location Returns a location Entity-Attribute dictionary """ self.CenterPoint = self.randomGenerator.CreateRandomLattitudeLongitude( ) self.SearchDistanceMiles = self.randomGenerator.CreateRandomNumber( self.minDistanceMiles, self.maxDistanceMiles) self.SearchDistanceMeters = GeoConversion.ConvertMilesToMeters( self.SearchDistanceMiles) QueryParameterDic = {} QueryParameterDic['SearchType'] = 'LocationByProximity' QueryParameterDic['MaxResults'] = self.maxSearchResults QueryParameterDic[ 'MaxDistanceMeters'] = GeoConversion.ConvertMilesToMeters( self.SearchDistanceMiles) QueryParameterDic['CenterPt'] = self.CenterPoint QueryParameterDic['HasShopsOnly'] = 'True' QueryParameterDic['FilterByTrueDistance'] = 'True' # Get a list of GpGridCells to search gpSearch = GpSearch() gpSearch.MaxSearchCellCount = self.MaxSearchCellCount centerGpPoint = GpPoint(self.CenterPoint[0], self.CenterPoint[1]) gpSearch.ComputeSearchListForMetersProximity(centerGpPoint, self.SearchDistanceMeters) self.FinalSearchResolution = gpSearch.FinalSearchResolution self.SearchGpGridCells = gpSearch.SearchCellList QueryParameterDic['SearchGridCells'] = self.SearchGpGridCells # Convert Query to JSON QueryJSON = DataConversion.dict_to_json(QueryParameterDic) print 'QueryJSON =' print QueryJSON resultsJSON = HttpInterface.get_data(self.HostAndPort, self.HttpPage, self.HttpAction, QueryJSON) if resultsJSON == "": # No entities found in search locationEntityAttributeDic = {} elif resultsJSON.startswith('Not found error'): locationEntityAttributeDic = {} else: results = DataConversion.json_to_dict(resultsJSON) locationClassEntityDic = results['c'] locationEntityAttributeDic = locationClassEntityDic['Location'] return locationEntityAttributeDic
def _GetGridPointsWithinRadius(self): """ Create a dictionary of GpGridPoints within distanceMeters from a center point. Scan through the GridPoints in the bounding box region. Add all in-bound points to the dictionary. Associate a weight with each GridPoint that is inversely proportional to its distance from the center point. """ radiusMeters = self.SearchBoundaryMeters self.gpGridPointsInBoundsDic = {} stepSize = MAXIMUM_RESOLUTION - self.MaxSearchResolution + 1 for latInt in range(self.SWCornerGridPoint.LattitudeInt,self.NWCornerGridPoint.LattitudeInt + stepSize, \ stepSize): for lonInt in range (self.SWCornerGridPoint.LongitudeInt,self.SECornerGridPoint.LongitudeInt + stepSize, \ stepSize): gridPoint = GpGridPoint() gridPoint.InitFromIntLatLon(latInt,lonInt,self.MaxSearchResolution) gpPoint = GpPoint(gridPoint.LattitudeFloat,gridPoint.LongitudeFloat) distanceMeters = GpMath.distance(self.CenterGpPoint,gpPoint) if distanceMeters <= radiusMeters: weight = self._WeightGridPointByDistance(distanceMeters) self.gpGridPointsInBoundsDic[gridPoint.ToShortString()] = weight