0

これは、Google App Engine のデータベースに保存する予定のツイートをストリーミングしようとしているプロジェクトのコードの一部です。座標の緯度と経度の値を取得できることが重要です。(これらは後でプロットする予定です。)

現在の結果は次のようになります...

'tweet text'@REDACTED 正直に言うとわかりません...Googleの製品として、あなたはそう思うでしょう。Web ストアに公式の拡張機能がある場合があります。\ '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 が None でない場合」と表示されている場所を変更して、座標が範囲内にあるかどうかを確認することです。つまり、緯度 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
4

3 に答える 3

2

地球上の 2 点の大圏距離に基づいて決定することができます。

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 度は伝統的に 10000 キロメートルを表すことに注意してください (メートルの古代の定義である私の知る限り)、半径 10 キロメートルを取得するには、単に 0.09 度を使用します。

于 2013-09-26T09:35:06.890 に答える
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
于 2013-09-26T09:22:55.297 に答える