Example #1
0
def dbtest():
    """
    This is a hidden url route not available through website GUI.

    It serves as a test for developer's to make sure that their
    database and environment are up and running correctly.

    If the dev does not get an error message when louding this URL,
    then it can be assumed that the environment is working.
    """
    from app.models import Location, Feature
    from app.weighter import Weightifier
    from app.geolocator import LocationWrap
    weightifier = Weightifier()
    location = Location.query.filter_by(
        name='Phoenix', countrycode='US').order_by('id').first()
    feature = Feature.query.first()
    wrap = LocationWrap(location)
    codes = weightifier._get_admin_codes(wrap, 5)
    names = weightifier._get_admin_names(codes)
    # wrap.set_adminnames(names)
    return render_template('dbtest.html',
                           first_location=location,
                           admin_codes=codes,
                           admin_names=names,
                           first_feature=feature)
Example #2
0
def dbtest():
    """
    This is a hidden url route not available through website GUI.

    It serves as a test for developer's to make sure that their
    database and environment are up and running correctly.

    If the dev does not get an error message when louding this URL,
    then it can be assumed that the environment is working.
    """
    from app.models import Location, Feature
    from app.weighter import Weightifier
    from app.geolocator import LocationWrap
    weightifier = Weightifier()
    location = Location.query.filter_by(
        name='Phoenix',
        countrycode='US').order_by('id').first()
    feature = Feature.query.first()
    wrap = LocationWrap(location)
    codes = weightifier._get_admin_codes(wrap, 5)
    names = weightifier._get_admin_names(codes)
    # wrap.set_adminnames(names)
    return render_template(
        'dbtest.html',
        first_location=location,
        admin_codes=codes,
        admin_names=names,
        first_feature=feature)
 def __init__(self):
     self.geocoder = Geocoder()
     self.geojsoner = GeoJSONer()
     self.weightifier = Weightifier()
     return
class Geolocator(object):
    """
    Master geolocation class

    Uses Geocoder and GeoJSONer and Weightifier to find coordinates for and
    apply weights to all tagged locations.
    """
    def __init__(self):
        self.geocoder = Geocoder()
        self.geojsoner = GeoJSONer()
        self.weightifier = Weightifier()
        return

    def _build_container(self, locations):
        """
        Builds a LocationHitsContainer from the given locations

        :param list locations: list of app.models.Location objects to geolocate

        :returns: LocationHitsContainer
        """
        container = LocationHitsContainer()
        for l in locations:
            container.append(self.geocoder.geocode(l))
        return container

    def _apply_weights(self, container, weights, accuracy):
        """
        Uses the Weightifier to apply weights to the container

        :param LocationHitsContainer container: container of locations
        :param bool weights: flag indicating if weights should be calculated or
        not
        :param int accuracy: level of accuracy to use when calculating weights
        (must be greater than 0 and less than or equal to 5)

        :returns: modified container
        """
        if weights:
            if accuracy > 5:
                accuracy = 5
            container = self.weightifier.gather_all_names(container, accuracy)
            container = self.weightifier.weightify(container)
        return container

    def _build_geojson(self, container):
        """
        Iterates through locations in container and builds GeoJSON file

        :param LocationHitsContainer container: container of locations

        :returns: None
        """
        for hits in container.hits:
            for l in hits:
                self.geojsoner.append(l)
        return

    def geolocate(self, locations, weights=True, accuracy=1):
        """
        Given a list of tagged locations from the NLP tagger, this will convert
        each location to a app.geolocator.LocationWrap, find the coordinates of
        each, apply weighting, and convert to geojson

        :param list locations: list of app.models.Location objects to geolocate
        :param bool weights: flag indicating if weights should be calculated or
        not (defaults to True)
        :param int accuracy: level of accuracy to use when calculating weights
        (defaults to 1) (must be greater than 0 and less than or equal to 5)
            * 1 - weights will be found for all matches to countrycode
            * 2 - weights will be found for all matches to the above and
            admin1code
            * 3 - weights will be found for all matches to the above and
            admin2code
            * 4 - weights will be found for all matches to the above and
            admin3code
            * 5 - weights will be found for all matches to the above and
            admin4code

        :returns: None
        """
        # build the container
        container = self._build_container(locations)
        # apply weights
        container = self._apply_weights(container, weights, accuracy)
        # build the geojson
        self._build_geojson(container)
        return

    def geojson(self):
        """
        Returns the geojson of all geolocated locations

        :returns: geojson.FeatureCollection
        """
        return self.geojsoner.geojson()

    def __repr__(self):
        return "<Geolocator()>"
Example #5
0
 def __init__(self):
     self.geocoder = Geocoder()
     self.geojsoner = GeoJSONer()
     self.weightifier = Weightifier()
     return
Example #6
0
class Geolocator(object):
    """
    Master geolocation class

    Uses Geocoder and GeoJSONer and Weightifier to find coordinates for and
    apply weights to all tagged locations.
    """

    def __init__(self):
        self.geocoder = Geocoder()
        self.geojsoner = GeoJSONer()
        self.weightifier = Weightifier()
        return

    def _build_container(self, locations):
        """
        Builds a LocationHitsContainer from the given locations

        :param list locations: list of app.models.Location objects to geolocate

        :returns: LocationHitsContainer
        """
        container = LocationHitsContainer()
        for l in locations:
            container.append(self.geocoder.geocode(l))
        return container

    def _apply_weights(self, container, weights, accuracy):
        """
        Uses the Weightifier to apply weights to the container

        :param LocationHitsContainer container: container of locations
        :param bool weights: flag indicating if weights should be calculated or
        not
        :param int accuracy: level of accuracy to use when calculating weights
        (must be greater than 0 and less than or equal to 5)

        :returns: modified container
        """
        if weights:
            if accuracy > 5:
                accuracy = 5
            container = self.weightifier.gather_all_names(container, accuracy)
            container = self.weightifier.weightify(container)
        return container

    def _build_geojson(self, container):
        """
        Iterates through locations in container and builds GeoJSON file

        :param LocationHitsContainer container: container of locations

        :returns: None
        """
        for hits in container.hits:
            for l in hits:
                self.geojsoner.append(l)
        return

    def geolocate(self, locations, weights=True, accuracy=1):
        """
        Given a list of tagged locations from the NLP tagger, this will convert
        each location to a app.geolocator.LocationWrap, find the coordinates of
        each, apply weighting, and convert to geojson

        :param list locations: list of app.models.Location objects to geolocate
        :param bool weights: flag indicating if weights should be calculated or
        not (defaults to True)
        :param int accuracy: level of accuracy to use when calculating weights
        (defaults to 1) (must be greater than 0 and less than or equal to 5)
            * 1 - weights will be found for all matches to countrycode
            * 2 - weights will be found for all matches to the above and
            admin1code
            * 3 - weights will be found for all matches to the above and
            admin2code
            * 4 - weights will be found for all matches to the above and
            admin3code
            * 5 - weights will be found for all matches to the above and
            admin4code

        :returns: None
        """
        # build the container
        container = self._build_container(locations)
        # apply weights
        container = self._apply_weights(container, weights, accuracy)
        # build the geojson
        self._build_geojson(container)
        return

    def geojson(self):
        """
        Returns the geojson of all geolocated locations

        :returns: geojson.FeatureCollection
        """
        return self.geojsoner.geojson()

    def __repr__(self):
        return "<Geolocator()>"