2013-09-26 195 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度。

相关问题