0

I am new to python and currently working with Json file and I need to determine the location of each tweet and represent the grid in which they are present with the count of tweets in each grid cell.I tried comparing the xmin, xmax, ymin and ymax values of the polygon but it doesn't seems to be working. Any sort of advice would be helpful. This the function which i have used to return the grid_id but it returns only 3 gird ids , implying that all the tweets were made from those 3 grids, which is not the case

lt =[]
def match_tweets():

    for d in dic.values():
        for x in lst:

            if ((x['xmin'][0]<= tweet_long[1][0][0] and tweet_long[1][0][0]<=x['xmax'][0]) and (x['ymin'][0]<=tweet_lat[0][0][0] and tweet_lat[0][0][0]<x['ymax'][0])):
                x['count']+=1
                return lst.append(x['grid_id'])

The data in dic looks like this

    {0: [[[-37.95935781]], [[144.92340088]], [['melbourne']]],
     1: [[[-37.95935781]], [[144.92340088]], [['melbourne']]],
     2: [[[-37.95935781]], [[144.92340088]], [['melbourne']]],
     3: [[[-37.95935781]], [[144.92340088]], [['melbourne']]],
     4: [[[-37.95935781]], [[144.92340088]], [['melbourne']]],
     5: [[[-37.94917514]], [[144.92501796]], [['tb', 'tasmania']]],

The data in lst looks like

    [{'id': 'A1',
    'xmin': [144.7],
    'xmax': [144.85],
    'ymin': [-37.65],
    'ymax': [-37.5],
    'coordinates': [[[144.7, -37.5],
     [144.85, -37.5],
     [144.85, -37.65],
     [144.7, -37.65],
     [144.7, -37.5]]],
     'count': 0},
  • 1
    Do you want to program the algorithm for point-in-polygon yourself or can you use a readily available package for that? – bugmenot123 Apr 10 at 8:20
  • can you add a short example of what is in dic and lst, also did you mean to append to lt? – Ian Turton Apr 10 at 8:21
  • @bugmenot123 I can use the readily available packages for like – never_trust_a_dead_man Apr 10 at 12:20
  • @IanTurton yeah , for dic it goes like this{ 0: [[[-37.95935781]], [[144.92340088]], [['melbourne']]], 1: [[[-37.95935781]], [[144.92340088]], [['melbourne']]], 2: [[[-37.95935781]], [[144.92340088]], [['melbourne']]],... and for lst the values are [{'id': 'A1', 'xmin': [144.7], 'xmax': [144.85], 'ymin': [-37.65], 'ymax': [-37.5], 'coordinates': [[[144.7, -37.5], [144.85, -37.5], [144.85, -37.65], [144.7, -37.65], [144.7, -37.5]]], 'count': 0},... – never_trust_a_dead_man Apr 10 at 12:22
1

First of all, your data structures are pretty horrible. If you can, try to lose all the lists of lists of lists... I picked the first elements of your list and dictionary below.

I recommend using Shapely.

from shapely.geometry import *

You can create Polygon geometries:

something = Polygon(lst_thing[0]['coordinates'][0])

You can create Point geometries:

something_else = Point(d[0][0][0][0], d[0][1][0][0])  # sweet jesus...

And you can determine relationships between geometries, for example checking if a Point is in or on the border of a Polygon:

something_else.intersects(something)  # -> True/False

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.