2013-09-26 109 views
0

這是我的一個項目中的一部分代碼,我正在試圖發佈推文,我計劃將其保存到Google App Engine的數據庫中。對我來說,能夠獲得座標的緯度和長度值是非常重要的。 (我打算在繪製這些。)if語句來檢查座標是否在一個範圍內

目前的結果是這個樣子......

「的鳴叫文本」 @刪節不知道,說實話......作爲谷歌的產品,你會也這樣覺得。他們可能在網上商店有官方擴展。 \'User Name'REDACTED \'Created at't2013-09-26 08:39:45 \'Created with'tTwitter for Android \'geo't {u'type':u'Point',u 'coordinates':[52.569001,-2.7846582]}'coordinates't {u'type':u'Point',u'coordinates':[-2.7846582,52.569001]}

我想做的是改變它說「如果status.coordinates不是無」來檢查座標是否在一個範圍內。即Lat 50-55和長0-5。

謝謝! :)

class CustomStreamListener(tweepy.StreamListener): 

    def on_status(self, status): 

     if status.coordinates is not None: 
      try: 
       print "'tweet text'%s\n\ 'User Name't%s\n\ 'Created at't%s\n\ 'Created with't%s\n\ 'geo't%s\ 'coordinates't%s" % (status.text, 
            status.author.screen_name, 
            status.created_at, 
            status.source, 
            status.geo, 
            status.coordinates) 
      except Exception, e: 
       print >> sys.stderr, 'Encountered Exception:', e 
       pass 

    def on_error(self, status_code): 
     print >> sys.stderr, 'Encountered error with status code:', status_code 
     return True # Don't kill the stream 

    def on_timeout(self): 
     print >> sys.stderr, 'Timeout...' 
     return True # Don't kill the stream 

回答

0

非常感謝@Alfe和@Hellsgate的幫助和建議。

下面的代碼工作(如上目前Hellsgate的代碼返回地理標記的鳴叫,但不通過座標篩選)。

以上的CustomStreamListener,你只需要添加import語句和OAuth的方法。下面的代碼將只能從箱返回鳴叫英國各地的搜索術語「稀有鑽石,蘋果白蘭地,不和諧」

class CustomStreamListener(tweepy.StreamListener): 

def check_coords(self, status): 
    if status.coordinates is not None: 
     lat = status.coordinates['coordinates'][1] 
     lng = status.coordinates['coordinates'][0]   
     return 49.7 < lat < 58.5 and -6 < lng < 2.1 

def on_status(self, status): 


    if self.check_coords(status): 
     # if check_coords returns true, the following will run 
     try: 
      print "'tweet text'%s\n\ 'User Name't%s\n\ 'Created at't%s\n\ 'Created with't%s\n\ 'geo't%s\ 'coordinates't%s" % (status.text, 
           status.author.screen_name, 
           status.created_at, 
           status.source, 
           status.geo, 
           status.coordinates) 
     except Exception, e: 
      print >> sys.stderr, 'Encountered Exception:', e 
      pass 

def on_error(self, status_code): 
    print >> sys.stderr, 'Encountered error with status code:', status_code 
    return True # Don't kill the stream 

def on_timeout(self): 
    print >> sys.stderr, 'Timeout...' 
    return True # Don't kill the stream 


streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60) 


print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),) 
"""For track=[""], put in words to search for. Commas separate individual terms IE"Applejack, Discord", 
to search for tweets containing more than one term, separate keywords with a space. IE "Rarity Diamonds" """ 
streaming_api.filter(follow=None, track=["Rarity Diamonds,Applejack,Discord"]) 
0

假設座標格式給出[緯度,經度]你可以檢查它們如下:

def check_coords(coords): 
    lat = coords[0] 
    lng = coords[1] 
    return 50 < lat < 55 and 0 < lng < 55 

這將返回True如果緯度50之間 - 55和經度介於0 - 5.如果任是他們定義的範圍之外,則函數將返回False

編輯:添加給你的類將使它看起來像這樣:

class CustomStreamListener(tweepy.StreamListener): 

    def on_status(self, status): 

     if status.coordinates is not None: 
      # coordinates_in_range will be either True or False 
      coordinates_in_range = self.check_coords(status.coordinates['coordinates']) 
      try: 
       print "'tweet text'%s\n\ 'User Name't%s\n\ 'Created at't%s\n\ 'Created with't%s\n\ 'geo't%s\ 'coordinates't%s" % (status.text, 
            status.author.screen_name, 
            status.created_at, 
            status.source, 
            status.geo, 
            status.coordinates) 
      except Exception, e: 
       print >> sys.stderr, 'Encountered Exception:', e 
       pass 

    def on_error(self, status_code): 
     print >> sys.stderr, 'Encountered error with status code:', status_code 
     return True # Don't kill the stream 

    def on_timeout(self): 
     print >> sys.stderr, 'Timeout...' 
     return True # Don't kill the stream 

    def check_coords(self, coords): 
     latitude, longitude = coords 
     return 50 < latitude < 55 and 0 < longitude < 55 
+0

我建議縮寫的名稱,並通過解包分配('緯度,經度= coordinates'),但另有我同意這個解決方案。 – Alfe

+0

非常感謝你,代碼運行(&是超級有用的),但它似乎無法訪問座標。這是座標數據提供的格式:{u'type':u'Point',u'coordinates':[52.569001,-2.7846582]} – William

+0

&這就是我的代碼目前的樣子... class def on_status(self,status): def check_coords(self,status): lat = status.coordinates [1] lng =狀態。座標[0] 返回49 William

1

您可能需要基於地球上的兩個great-circle distance點決定:

from math import * 

def great_circle_distance(coordinates1, coordinates2): 
    latitude1, longitude1 = coordinates1 
    latitude2, longitude2 = coordinates2 
    d = pi/180 # factor to convert degrees to radians 
    return acos(sin(longitude1*d) * sin(longitude2*d) + 
       cos(longitude1*d) * cos(longitude2*d) * 
       cos((latitude1 - latitude2) * d))/d 

def in_range(coordinates1, coordinates2, range): 
    return great_circle_distance(coordinates1, coordinates2) < range 

請記住,90度的地球代表傳統萬公里(據我所知這是米的古老定義),所以要得到10km的半徑,只需使用0.09度。

相關問題