T
T
toshinogorlo2015-05-31 23:18:16
Python
toshinogorlo, 2015-05-31 23:18:16

Using Python with the VK API, how can I get check-ins from a specific street?

I'm trying to use the VK API places.getCheckins method to get checkins from a specific street for later use in analysis (of sentiment?). This method requires the specified latitude and longitude as parameters. I downloaded the coordinates of some streets in Moscow in GeoJSON format. Looks like this:

{
 ...
 "properties": { 
     ...
     "name": "a-street-so-called", 
     ... 
  }, 
  "geometry": { 
     "type": "LineString", 
     "coordinates": [ 
         [ 37.399092526176915, 55.715745258737407 ], 
         [ 37.398983226159537, 55.715823964808216 ] 
     ] 
  } 
 }

And left them here (166 MB).
From here I get the coordinates of a specific street using a script:
def get_streets(name):
    coordinates = []
    for i in data['features']:
        try:
            if i['properties']['name'] == name:
                coordinates.append(i['geometry']['coordinates'])
        except:
            None
     return coordinates

The result looks something like this (I represent it as "type": "LineString" in GeoJSON format):
[
    [37.625916884336014, 55.67560424062041], 
    [37.62689513625539, 55.67304407211511], 
    [37.62689513625539, 55.67304407211511], 
    [37.627487820628794, 55.671551422797954], 
    [37.63091308536064, 55.66356606746359], 
    [37.631465368960754, 55.663102380580035],
    ...
]

Or if the file contains a street more than once ("type":"MultiLineString"), then:
[
    [
        [37.625916884336014, 55.67560424062041], 
        [37.62689513625539, 55.67304407211511]
    ], 
    [
        [37.62689513625539, 55.67304407211511], 
        [37.627487820628794, 55.671551422797954],
        ...
    ], 
    [
        [37.63091308536064, 55.66356606746359], 
        [37.631465368960754, 55.663102380580035],
        ...
    ],
...
]

There are gaps between coordinates on the map on straight sections of roads:
lmGT5.png
I try to fill them linearly:
def calc_points(lat_0, lon_0, lat_1, lon_1):
    """
    The function takes in two coordinates and returns a
    list of new coordinates, which lie on the line between
    the first two.
    """
    new_points = []
    y_displacement = lat_0 - lat_1
    x_displacement = lon_0 - lon_1
    # Using the formula for a line: y = m * x + b.
    m = y_displacement / x_displacement
    b = lat_0 - m * lon_0
    x = lon_0
    if lon_1 > lon_0:
        while x < lon_1:
            x += 0.00001
            lat_new = round(m * x + b, 6)
            new_points.append((x, lat_new))
    elif lon_0 > lon_1:
        while x > lon_1:
            x -= 0.00001
            lat_new = round(m * x + b, 6)
            new_points.append((x, lat_new))
    return new_points

After I try to automate all this:
def calc_streets(coordinates):
    j = 0
    # check if the coordinates list is nested
    if coordinates[0][0] != None:
        for i in coordinates:
            threshold = len(i) - 1
            while j < threshold:
                new = calc_points(i[j][1],
                                  i[j][0],
                                  i[j+1][1],
                                  i[j+1][0])
                coordinates.append(new)
                j += 1
    else:
        threshold = len(coordinates) - 1
        while j < threshold:
            new = calc_points(coordinates[j][1],
                              coordinates[j][0],
                              coordinates[j+1][1],
                              coordinates[j+1][0])
            coordinates.append(new)
            j += 1

All this makes me wonder if there is an easier way to get check-ins? Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question