简体   繁体   English

作业帮助:Python如何使用long和lat确定推文的来源

[英]Homework Help: Python how to determine origin of a tweet using long and lat

REALLY confused about a section from my homework, its not graded or anything but I still want to be able to complete and understand it. 我对作业中的某个部分感到非常困惑,该部分没有评分或其他任何内容,但我仍然希望能够完成并理解它。

I'm trying to "approximate" regions corresponding to their timezones (not real data). 我正在尝试“近似”与其时区相对应的区域(不是真实数据)。 So Eastern ( P1. P2. P3, P4 , Pacific( P7, P8, P9, P10 ), Mountain ( P5, P6, P7, P8 ) , Central( P3,P4,P5,P6 ).... 所以东部( P1。P2。P3,P4 ,太平洋( P7,P8,P9,P10 ),山区( P5,P6,P7,P8 ),中央( P3,P4,P5,P6 )....

im trying to determine the origin of a tweet, by determining which region the latitude and longitude of the tweet belongs (I have a txt file and all the tweets have a long, lat number associated with it. for example: [36.970152730000002, -84.090040380000005] 4 2011-09-02 17:40:11 Chicken flatheads at taco bell my life is complete) . 我试图通过确定该推文的纬度和经度所属的区域来确定该推文的来源(我有一个txt文件,所有推文均具有与之相关的[36.970152730000002, -84.090040380000005] 4 2011-09-02 17:40:11 Chicken flatheads at taco bell my life is complete)和经度编号。例如: [36.970152730000002, -84.090040380000005] 4 2011-09-02 17:40:11 Chicken flatheads at taco bell my life is complete) The values of the points are: (rectangle) 点的值是:(矩形)

p1 = (49.189787, -67.444574) 
p2 = (24.660845, -67.444574)
p3 = (49.189787, -87.518395) 
p4 = (24.660845, -87.518395) 
p5 = (49.189787, -101.998892)
p6 = (24.660845, -101.998892) 
p7 = (49.189787, -115.236428) 
p8 = (24.660845, -115.236428) 
p9 = (49.189787, -125.242264)
p10 = (24.660845, -125.242264)

For example, if a tweet is inbetween p1, p2, p3, p4 then they are from Eastern. 例如,如果一条推文介于p1,p2,p3,p4之间,则它们来自东部。 How would I do this? 我该怎么做?

Yes, this is apart of homework, and I know sometimes you guys dont like assissting with homework especially if nothing is provided. 是的,这是家庭作业的一部分,我知道有时候你们不喜欢协助家庭作业,尤其是如果没有提供任何帮助。 But I emailed my teacher 5 hours ago and he stil hasn't responded even though he promised he would reply by tonight (12:48am here) 但是我5个小时前给我的老师发了电子邮件,尽管他承诺他会在今晚(12:48 am)答复,但他仍未回应。

Answers would be appreciated with explanations but even comments would help :) Its a long assignment and I finished 95% just this part is needed..but im just confused as to the long, lat business. 答案会带有解释,不胜感激,但即使发表评论也将有所帮助:)它是一项长期的工作,我完成了95%的工作,仅此部分是必需的..但我对长期的经商感到困惑。

Assuming the regions are rectangles. 假设区域是矩形。

Just check the value of the lat, long against the "border" values. 只需检查lat的值,并与“ border”值比较长即可。

So, for Eastern: 因此,对于东方:

if lat >= 24.660845 and lat < 49.189787 and long <= -67.444574 and long > -87.518395:
    return True 

Or, if the regions are sequential as you've shown (and only changing in longitude), just check that. 或者,如果您所显示的区域是连续的(并且仅在经度上发生变化),则只需检查一下即可。

Of course, if the regions are irregular polygons, it would be a whole new problem. 当然,如果区域是不规则的多边形,那将是一个全新的问题。

Edit : One approach would be to create a Region class with a contains(lat, long) method: 编辑 :一种方法是使用contains(lat,long)方法创建Region类:

class Region:
    def __init__(self, lat_tuple, long_tuple):
        self.lat_tuple = lat_tuple
        self.long_tuple = long_tuple

    def contains(self, lat, long):
        return self.lat_tuple[0] <= lat and lat < self.lat_tuple[1] and\
               self.long_tuple[0] <= long and long < self.long_tuple[1]

And use it as: 并将其用作:

eastern = Region((24.660845, 49.189787), (-87.518395, -67.444574))
print(eastern.contains(34.0, - 70.10))
print(eastern.contains(14.0, - 70.10))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM