예제 #1
0
    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]
class GpGridCell():
    Origin = GpGridPoint()
    
    def InitFromGridPoint(self,gpGridPoint):
        self.Origin.InitFromIntLatLon(gpGridPoint.LattitudeInt,gpGridPoint.LongitudeInt,gpGridPoint.Resolution)

    def InitFromCoordinates(self, lattitude, longitude, resolution):
        self.Origin.InitFromCoordinates( lattitude, longitude, resolution)

    def InitFromShortCellString(self, cellShortString, resolution):
        self.Origin = GpGridPoint()
        self.Origin.InitFromShortString(cellShortString, resolution)
        
    def ToLongString(self):
        """ Return a long string representation of this GridCell.

        The cell is identified by the long string representation of its origin."""
        return self.Origin.ToLongString()

    def ToShortString(self):
        """ Return a short string representation of this GridCell.

        The cell is identified by the short string representation of its origin along with
        its resolution."""
        return self.Origin.ToShortString()

    def ListCornerPointsShort(self):
        """Return a list of the four corner points for this cell in the short string format.

        The points are identified by their short string representation"""
        cellList= []
        cellList.append(self.Origin.ToShortString())
        nwPoint = self.Origin.GetAdjacentPoint('N')
        cellList.append(nwPoint.ToShortString())
        nePoint = nwPoint.GetAdjacentPoint('E')
        cellList.append(nePoint.ToShortString())
        sePoint = nePoint.GetAdjacentPoint('S')
        cellList.append(sePoint.ToShortString())
        return cellList

    def ListChildCellsShort(self):
        """ Return a list of the 4 child cells in GpGridPointShortString format"""
        # Get the origin point of child cell that is co-located with the Origin Point
        parentResolution = self.Origin.Resolution
        if parentResolution == MAXIMUM_RESOLUTION:
            logging.debug("GpGridCell is at maximum resolution %d, so there are no child cells" %\
                             parentResolution)
            return []
        childOriginGridPoint = GpGridPoint()
        childOriginGridPoint.InitFromIntLatLon(self.Origin.LattitudeInt,self.Origin.LongitudeInt,\
                                               self.Origin.Resolution +1)
        # Create a GpGridCell at the child origin
        swChildGridCell = GpGridCell()
        swChildGridCell.InitFromGridPoint(childOriginGridPoint)
        # The 4 corner points of the SW child GridCell are the origins of the 4 child cells
        return swChildGridCell.ListCornerPointsShort()