尝试使用geopy返回的纬度和经度来创建shapefile。如果我给它一组数字(44.977753,-93.2650108),shapefile创建器部分工作,但它不能用于返回的数据lat_long。我的想法是它需要一个“,”但我不知道。
from geopy.geocoders import GoogleV3
import csv
import ogr, os
def geopy():
loc = raw_input("What location? ")
geolocator = GoogleV3()
location = geolocator.geocode(loc, exactly_one=True)
if location != None:
Address = location.address
lat_long = location.latitude, location.longitude
latitude = str(location.latitude)
longitude = str(location.longitude)
print Address, latitude, longitude
print""
else:
print "There is no geographic information to return for the word in input. \n"
# Input data
pointCoord = lat_long
fieldName = 'test'
fieldType = ogr.OFTString
fieldValue = 'test'
outSHPfn = "output file"
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# Create the output shapefile
shpDriver = ogr.GetDriverByName("ESRI Shapefile")
if os.path.exists(outSHPfn):
shpDriver.DeleteDataSource(outSHPfn)
outDataSource = shpDriver.CreateDataSource(outSHPfn)
outLayer = outDataSource.CreateLayer(outSHPfn, srs, geom_type = ogr.wkbPoint )
#create point geometry
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(pointCoord[0],pointCoord[1])
# create a field
idField = ogr.FieldDefn(fieldName, fieldType)
outLayer.CreateField(idField)
# Create the feature and set values
featureDefn = outLayer.GetLayerDefn()
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(point)
outFeature.SetField(fieldName, fieldValue)
outLayer.CreateFeature(outFeature)
geopy()
答案 0 :(得分:0)
需要添加循环以将纬度和经度放入列表中。此代码将创建您提供的任何位置的点shapefile。
from geopy.geocoders import GoogleV3
import csv
import ogr, os
def geopy(location):
"""This function takes the word given about
and uses GoogleV3 to search for a location. If a
location is found it then returns the Address, latitude and longitude.
It then prints that information to a .CSV"""
geolocator = GoogleV3()
loc_input = raw_input("Add the location you would like data back for: ")
location = geolocator.geocode(loc_input, exactly_one=True)
if location != None:
Address = location.address
lat_lon = location.latitude, location.longitude
latitude = str(location.latitude)
longitude = str(location.longitude)
print Address, latitude, longitude
print""
#Converts lat_long to a list for use in making the shapefile.
list_lat = []
for i in range(1):
list_lat.append(lat_lon)
for list_of_lat_lon in list_lat:
print""
#Calls list_of_lat_lon for the shapefile function
shapefile(list_of_lat_lon)
# If there is no location data to return it prints the below line and does not create a shapefile
else:
print "There is no geographic information to return for the word in input. \n"
def shapefile(list_of_lat_lon):
"""This function uses the GDAL to return a ESRi shapefile
it uses the latitude and longitude in the list_of_lat_lon list.
"""
# Input data
pointCoord = list_of_lat_lon
fieldName = 'Lat'
fieldType = ogr.OFTString
fieldValue = 'test'
outSHPfn = "Input file location"
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# Create the output shapefile
shpDriver = ogr.GetDriverByName("ESRI Shapefile")
if os.path.exists(outSHPfn):
shpDriver.DeleteDataSource(outSHPfn)
outDataSource = shpDriver.CreateDataSource(outSHPfn)
outLayer = outDataSource.CreateLayer(outSHPfn, srs, geom_type = ogr.wkbPoint )
#create point geometry longitude first then latitude
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(pointCoord[1],pointCoord[0])
# create a field
idField = ogr.FieldDefn(fieldName, fieldType)
outLayer.CreateField(idField)
# Create the feature and set values
featureDefn = outLayer.GetLayerDefn()
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(point)
outFeature.SetField(fieldName, fieldValue)
outLayer.CreateFeature(outFeature)
geopy(location)