本文整理匯總了Python中geopy.geocoders.GoogleV3方法的典型用法代碼示例。如果您正苦於以下問題:Python geocoders.GoogleV3方法的具體用法?Python geocoders.GoogleV3怎麽用?Python geocoders.GoogleV3使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類geopy.geocoders
的用法示例。
在下文中一共展示了geocoders.GoogleV3方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_pos_by_name
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def get_pos_by_name(self, location_name):
# Check if given location name, belongs to favorite_locations
favorite_location_coords = self._get_pos_by_fav_location(location_name)
if favorite_location_coords is not None:
return favorite_location_coords
# Check if the given location is already a coordinate.
if ',' in location_name:
possible_coordinates = re.findall(
"[-]?\d{1,3}(?:[.]\d+)?", location_name
)
if len(possible_coordinates) >= 2:
# 2 matches, this must be a coordinate. We'll bypass the Google
# geocode so we keep the exact location.
self.logger.info(
'[x] Coordinates found in passed in location, '
'not geocoding.'
)
return float(possible_coordinates[0]), float(possible_coordinates[1]), (float(possible_coordinates[2]) if len(possible_coordinates) == 3 else self.alt)
geolocator = GoogleV3(api_key=self.config.gmapkey)
loc = geolocator.geocode(location_name, timeout=10)
return float(loc.latitude), float(loc.longitude), float(loc.altitude)
示例2: set_location
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def set_location(location_name):
geolocator = GoogleV3()
prog = re.compile('^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$')
global origin_lat
global origin_lon
if prog.match(location_name):
local_lat, local_lng = [float(x) for x in location_name.split(",")]
alt = 0
origin_lat, origin_lon = local_lat, local_lng
else:
loc = geolocator.geocode(location_name)
origin_lat, origin_lon = local_lat, local_lng = loc.latitude, loc.longitude
alt = loc.altitude
print '[!] Your given location: {}'.format(loc.address.encode('utf-8'))
print('[!] lat/long/alt: {} {} {}'.format(local_lat, local_lng, alt))
set_location_coords(local_lat, local_lng, alt)
示例3: get_pos_by_name
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def get_pos_by_name(self, location_name):
# Check if given location name, belongs to favorite_locations
favorite_location_coords = self._get_pos_by_fav_location(location_name)
if favorite_location_coords is not None:
return favorite_location_coords
# Check if the given location is already a coordinate.
if ',' in location_name:
possible_coordinates = re.findall(
"[-]?\d{1,3}[.]\d{3,7}", location_name
)
if len(possible_coordinates) >= 2:
# 2 matches, this must be a coordinate. We'll bypass the Google
# geocode so we keep the exact location.
self.logger.info(
'[x] Coordinates found in passed in location, '
'not geocoding.'
)
return float(possible_coordinates[0]), float(possible_coordinates[1]), (float(possible_coordinates[2]) if len(possible_coordinates) == 3 else self.alt)
geolocator = GoogleV3(api_key=self.config.gmapkey)
loc = geolocator.geocode(location_name, timeout=10)
return float(loc.latitude), float(loc.longitude), float(loc.altitude)
示例4: query_google
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def query_google(latitude, longitude):
coordinates = "%s, %s" % (latitude, longitude)
Logger.log_more_verbose(
"Querying Google Geocoder for: %s" % coordinates)
try:
g = geocoders.GoogleV3()
r = g.reverse(coordinates)
if r:
return r[0][0].encode("UTF-8")
except Exception, e:
fmt = traceback.format_exc()
Logger.log_error_verbose("Error: %s" % str(e))
Logger.log_error_more_verbose(fmt)
#--------------------------------------------------------------------------
示例5: __init__
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def __init__(self):
self.geocoder = GoogleV3()
self.updated_tweets = []
示例6: geocode_location
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def geocode_location(location):
"""Geocode a location string using Google geocoder."""
geocoder = geocoders.GoogleV3()
try:
result = geocoder.geocode(location, exactly_one=False)
except Exception:
return None, None
try:
ctr_lat, ctr_lng = result[0][1]
except IndexError:
return None, None
return clean_coords(coords=(ctr_lat, ctr_lng))
示例7: get_pos_by_name
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def get_pos_by_name(location_name):
geolocator = GoogleV3()
loc = geolocator.geocode(location_name, timeout=10)
if not loc:
return None
log.info("Location for '%s' found: %s", location_name, loc.address)
log.info('Coordinates (lat/long/alt) for location: %s %s %s', loc.latitude, loc.longitude, loc.altitude)
return (loc.latitude, loc.longitude, loc.altitude)
示例8: get_pos_by_name
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def get_pos_by_name(location_name):
geolocator = GoogleV3()
loc = geolocator.geocode(location_name, timeout=10)
logger.debug('location: %s', loc.address.encode('utf-8'))
logger.debug('lat, long, alt: %s, %s, %s', loc.latitude, loc.longitude, loc.altitude)
return (loc.latitude, loc.longitude, loc.altitude), loc.address.encode('utf-8')
示例9: login
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def login(self, provider, username, password):
# login needs base class "create_request"
self.useVanillaRequest = True
# Get Timecode and Country Code
country_code = "US"
timezone = "America/Chicago"
geolocator = GoogleV3(api_key=self.config.gmapkey)
if self.config.locale_by_location:
try:
location = geolocator.reverse((self.actual_lat, self.actual_lng), timeout = 10, exactly_one=True)
country_code = self.get_component(location,'country')
except:
self.logger.warning("Please make sure you have google api key and enable Google Maps Geocoding API at console.developers.google.com")
try:
timezone = geolocator.timezone([self.actual_lat, self.actual_lng], timeout=10)
except:
self.logger.warning("Please make sure you have google api key and enable Google Maps Time Zone API at console.developers.google.com")
# Start login process
try:
if self.config.proxy:
PGoApi.set_authentication(
self,
provider,
username=username,
password=password,
proxy_config={'http': self.config.proxy, 'https': self.config.proxy}
)
else:
PGoApi.set_authentication(
self,
provider,
username=username,
password=password
)
except:
raise
try:
if self.config.locale_by_location:
response = PGoApi.app_simulation_login(self,country_code,timezone.zone)
else:
response = PGoApi.app_simulation_login(self) # To prevent user who have not update the api being caught off guard by errors
except BadHashRequestException:
self.logger.warning("Your hashkey seems to have expired or is not accepted!")
self.logger.warning("Please set a valid hash key in your auth JSON file!")
exit(-3)
raise
except BannedAccountException:
self.logger.warning("This account is banned!")
exit(-3)
raise
except:
raise
# cleanup code
self.useVanillaRequest = False
return response
示例10: geocode
# 需要導入模塊: from geopy import geocoders [as 別名]
# 或者: from geopy.geocoders import GoogleV3 [as 別名]
def geocode(self, resource):
"""
Performs geocoding on the provided resource and updates its formatted
address, latitude, and longitude.
Args:
resource: A resource from which the address components will be
retrieved and used to update its formatted address,
latitude, and longitude.
Raises:
geopy.exc.GeopyError: An error occurred attempting to access the
geocoder.
"""
# Make sure we generated something meaningful
if resource.address is not None and not resource.address.isspace():
# Now query the geocoder with this formatted string
geolocator = GoogleV3(api_key=self.api_key)
new_location = geolocator.geocode(
resource.address,
exactly_one=True)
if new_location is not None:
new_res_location = ''
if new_location.address and \
not new_location.address.isspace():
resource.address = new_location.address
if new_location.latitude is not None and \
new_location.longitude is not None:
resource.latitude = new_location.latitude
resource.longitude = new_location.longitude
# Look at the raw response for address components
if new_location.raw:
address_components = new_location.raw.get(
'address_components')
if address_components:
# Get the city/county/state strings
city_str, county_str, state_str = \
self.get_locality_strings(address_components)
# Now build the location based on those strings,
# preferring city to county
new_res_location = city_str or county_str or ''
if state_str:
# Add a comma and space between the city/county
# and state
if new_res_location:
new_res_location = new_res_location + ', '
new_res_location = new_res_location + state_str
# After all of that, update the location
resource.location = new_res_location
pass