1

Below is the section of code I'm having trouble with. How do I designate the x and y coordinates from the (lat, lng) variable in the twittersearch for loop in the top part? These coordinates will be inserted into a feature class/shapefile to then be loaded into ArcGIS as point data.

for tweet in ts.search_tweets_iterable(tso):
        print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode('ascii', 'ignore') 
        #ignore unicode here, show error on map??
        if tweet['place'] is not None:
            (lat, lng) = geo(tweet['place']['full_name'])
            print 'And they said it from (' + str(lat) +', ' + str(lng) + ')'
        else:
            print "And their place wasn't specified..."

    with arcpy.da.InsertCursor("D:/UW/Winter_2016/501/w7_twitter/twitterSearchFC.shp", ["SHAPE@XY"]) as cursor:
        x = float(lat).xcor()
        y = float(lng).ycor()
        varPoint = arcpy.Point(x,y)   
        array.add(varPoint)
        twitterPoint = arcpy.Point(array)
        cursor.insertRow([twitterPoint])

After running the script this is what I get from Twitter, if that helps.

@12voltarts tweeted: #chihuly bowl @ Chihuly Garden and Glass https://t.co/Hf0RqE5nU3 And they said it from (47.6038321, -122.3300623)

@twanderingeater tweeted: [new post] @ChihulyGG the home of many gorgeous #Chihuly pieces in #Seattle https://t.co/OPPwvd7hQ2 https://t.co/C4HSdHZs5c And their place wasn't specified...

My thinking is that I will need to identify each lat and lng as a new variable and call that in the cursor piece. I'm almost positive there is an easier way to do this...

also the error:

x = float(lat).xcor() AttributeError: 'float' object has no attribute 'xcor'

from geopy import geocoders

def geo(location):
    g = geocoders.Nominatim()
    loc = g.geocode(location)
    return float(loc.latitude), float(loc.longitude)

output is a shapefile with twitter location data (tweets) spatially referenced to be able to see it in ArcGIS

  • What library is the geo function from? – user2856 Feb 27 '16 at 22:50
  • geopy is the library geo is a function that is defined, code above has been updated – Staley Feb 27 '16 at 23:01
2

Firstly, the error AttributeError: 'float' object has no attribute 'xcor' is self-explanatory. Python float objects do not have an xcor method or property.

>>> print [a for a in dir(1.0) if not a.startswith('_')]
['as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']

I'm not quite sure what you are doing with the insert cursor code and the array (are you trying to make a MultiPoint featureclass? Maybe edit your question again to describe in words what your output should be), but something like the following might work. Note I've pulled the insert cursor out of the loop as repeatedly creating cursors is way slow. Do it once and reuse it in each iteration:

with arcpy.da.InsertCursor("D:/UW/Winter_2016/501/w7_twitter/twitterSearchFC.shp", ["SHAPE@XY", "USER", "TWEET"]) as cursor:

    for tweet in ts.search_tweets_iterable(tso):

        print( '@%s tweeted: %s' % ( tweet['user']['screen_name'], tweet['text'] ) ).encode('ascii', 'ignore') 
        #ignore unicode here, show error on map??
        if tweet['place'] is not None:
            (lat, lng) = geo(tweet['place']['full_name'])
            print 'And they said it from (' + str(lat) +', ' + str(lng) + ')'
            varPoint = arcpy.Point(lng,lat)   
            cursor.insertRow([varPoint, tweet['user']['screen_name'], tweet['text']])
        else:
            print "And their place wasn't specified..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.