Beispiel #1
0
    def parseFormData(self, data):
        if self.isEmptyDatamodel(data):
            return None

        if isinstance(data, Geo):
            return data

        if not isinstance(data, dict):
            raise WidgetError('Expected multiple values for "%s"' % self.title)

        try:
            lat = data.get('lat', None)
            if not lat:
                lat = None
            lon = data.get('lon', None)
            if not lon:
                lon = None
            address = data.get('address', '').strip()
            if address and lat is lon is None:
                coordinates = geocoding.location_geocode(address)
                if coordinates is not None:
                    lat, lon = coordinates
            return Geo(lat, lon, address)
        except ValueError, e:
            raise WidgetError(str(e))
Beispiel #2
0
    def parseFormData(self, data):
        if self.isEmptyDatamodel(data):
            return None

        if isinstance(data, Geo):
            return data

        if not isinstance(data, dict):
            raise WidgetError('Expected multiple values for "%s"' % self.title)

        try:
            lat = data.get('lat', None)
            if not lat:
                lat = None
            lon = data.get('lon', None)
            if not lon:
                lon = None
            address = data.get('address', '').strip()
            if address and lat is lon is None:
                coordinates = geocoding.location_geocode(address)
                if coordinates is not None:
                    lat, lon = coordinates
            return Geo(lat, lon, address)
        except ValueError, e:
            raise WidgetError(str(e))
Beispiel #3
0
def do_geocoding(address):
    """ """
    #if self.portal_map.current_engine == 'yahoo':
    #    coordinates = geocoding.yahoo_geocode(address)
    #elif self.portal_map.current_engine == 'google':
    coordinates = geocoding.location_geocode(address)
    if coordinates != None:
        lat, lon = coordinates
        return Geo(lat, lon, address)
    return (None)
Beispiel #4
0
 def do_geocoding(self, properties):
     lat = properties.get(self.geo_fields['lat'], '')
     lon = properties.get(self.geo_fields['lon'], '')
     address = properties.get(self.geo_fields['address'], '')
     if lat.strip() == '' and lon.strip() == '' and address:
         if self.portal_map.map_engine == 'yahoo':
             coordinates = geocoding.yahoo_geocode(address)
         elif self.portal_map.map_engine == 'google':
             coordinates = geocoding.location_geocode(address)
         if coordinates != None:
             lat, lon = coordinates
             properties[self.geo_fields['lat']] = lat
             properties[self.geo_fields['lon']] = lon
     return properties
def set_address(obj):
    """ Set the postal address based on geo address and viceversa
    """
    # convert postaladdress to geolocation
    if obj.postaladdress and not obj.geo_location:
        try:
            lat, lon = geocoding.location_geocode(obj.postaladdress)
            obj.geo_location = Geo(lat, lon, obj.postaladdress)
        except:
            pass

        return

    # convert geolocation to postal address
    lang = 'en'
    v = obj.getLocalAttribute("postaladdress", lang)
    if obj.geo_location and not v:
        obj.set_localpropvalue('postaladdress', lang, obj.geo_location.address)
        return
Beispiel #6
0
def set_address(obj):
    """ Set the postal address based on geo address and viceversa
    """
    # convert postaladdress to geolocation
    if obj.postaladdress and not obj.geo_location:
        try:
            lat, lon = geocoding.location_geocode(obj.postaladdress)
            obj.geo_location = Geo(lat, lon, obj.postaladdress)
        except:
            pass

        return

    # convert geolocation to postal address
    lang = 'en'
    v = obj.getLocalAttribute("postaladdress", lang)
    if obj.geo_location and not v:
        obj.set_localpropvalue('postaladdress', lang, obj.geo_location.address)
        return
Beispiel #7
0
def geolocate_queue(site):
    for site_path in list(site.geolocation_queue):
        try:
            obj = site.unrestrictedTraverse(site_path)
        except KeyError:
            # the object is not there anymore
            site.geolocation_queue.remove(site_path)
            site._p_changed = True
            LOG.debug('object not found %s' % site_path)
            transaction.commit()
            continue
        lat = obj.geo_location.lat
        lon = obj.geo_location.lon
        address = obj.geo_location.address
        if address and not (lat and lon):
            try:
                # Google also restricts requests per second, so we need
                # to be careful for this, too
                time.sleep(2)
                lat, lon = geocoding.location_geocode(address)
                if lat and lon:
                    obj.geo_location = Geo(lat, lon, address)
                site.geolocation_queue.remove(site_path)
                site._p_changed = True
                LOG.info('coodrdinates %s and %s found for %s' %
                         (lat, lon, address))
                transaction.commit()
            except GeocoderServiceError, e:
                if 'ZERO_RESULTS' in e.message:
                    LOG.info('coodrdinates not found for %s' % address)
                    site.geolocation_queue.remove(site_path)
                else:
                    LOG.info(e)
                    site.previous_geolocation = datetime.now()
                    site._p_changed = True
                    break
        else:
            LOG.info('object already geolocated %s' % site_path)
            site.geolocation_queue.remove(site_path)
            site._p_changed = True
            transaction.commit()
    def getDatamodel(self, form):
        """Get datamodel from form"""
        lat = form.get(self.getWidgetId() + '.lat', None)
        lon = form.get(self.getWidgetId() + '.lon', None)
        address = form.get(self.getWidgetId() + '.address', '')
        if not (lat and lon):
            coordinates = geocoding.location_geocode(address)
            if coordinates is not None:
                lat, lon = coordinates
        if not lat:
            lat = None
        if not lon:
            lon = None

        if lat is None and lon is None and address == '':
            return None

        try:
            return Geo(lat, lon, address)
        except ValueError:
            raise WidgetError('Invalid geo values for "%s"' % self.title)
Beispiel #9
0
    def getDatamodel(self, form):
        """Get datamodel from form"""
        lat = form.get(self.getWidgetId() + '.lat', None)
        lon = form.get(self.getWidgetId() + '.lon', None)
        address = form.get(self.getWidgetId() + '.address', '')
        if not (lat and lon):
            coordinates = geocoding.location_geocode(address)
            if coordinates is not None:
                lat, lon = coordinates
        if not lat:
            lat = None
        if not lon:
            lon = None

        if lat is None and lon is None and address == '':
            return None

        try:
            return Geo(lat, lon, address)
        except ValueError:
            raise WidgetError('Invalid geo values for "%s"' % self.title)
Beispiel #10
0
 def do_geocoding(self, address):
     coordinates = geocoding.location_geocode(address)
     if coordinates != None:
         return coordinates
     return ('', '')
Beispiel #11
0
 def do_geocoding(self, address):
     coordinates = geocoding.location_geocode(address)
     if coordinates != None:
         return coordinates
     return ('', '')