Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
toon committed Feb 10, 2018
0 parents commit 94fd89a
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# OpenCellId scripts
Bunch of scripts to request [opencellid](http://opencellid.org/).

## area.py
This is a quick & dirty script to get radio cells in a square area with around
a coordinate.

The requests are limited to 5000 requests per day (free api key).

### Parameters
* -h, --help: prints help
*
* -p, --position: coordinates of position in center of ara.
Format: lat<float>,lon<float>
* -a, --area: size of the areai in km². This is limited to 400,000 ft² by
opencellid.
Format: area<float>

### Example

```bash
$ ./area.py -p 51.0824,13.7255 -a 0.1 -k SCRTKEY
25 Stations Found
cellid | lat , lon | lac | mcc |mnc| radio
----------+----------------------+-------+-----+---+------
64492 | 51.081174, 13.724188 | 35077 | 262 | 1 | GSM
134555761 | 51.081325, 13.727417 | 40053 | 262 | 3 | UMTS
9761 | 51.082741, 13.726536 | 350 | 262 | 2 | GSM
27086850 | 51.082926, 13.725547 | 18000 | 262 | 1 | LTE
63123 | 51.083102, 13.726052 | 773 | 262 | 3 | GSM
213574 | 51.083130, 13.726318 | 35221 | 262 | 1 | UMTS
48156731 | 51.083296, 13.725373 | 21634 | 262 | 7 | UMTS
1079656 | 51.083343, 13.727417 | 35221 | 262 | 1 | UMTS
48146731 | 51.083413, 13.727417 | 21634 | 262 | 7 | UMTS
1065542 | 51.083554, 13.727417 | 35221 | 262 | 1 | UMTS
24 | 51.083679, 13.727417 | 35992 | 262 | 1 | GSM
28 | 51.083679, 13.727417 | 35813 | 262 | 1 | GSM
64492 | 51.083679, 13.727417 | 35077 | 262 | 3 | GSM
3976 | 51.083679, 13.727417 | 35082 | 262 | 3 | GSM
41 | 51.083679, 13.727417 | 3108 | 262 | 2 | GSM
48764797 | 51.083679, 13.727417 | 21609 | 262 | 7 | UMTS
43307 | 51.083679, 13.727417 | 21634 | 262 | 7 | GSM
27312 | 51.083679, 13.727417 | 350 | 262 | 1 | GSM
7831809 | 51.083679, 13.727417 | 50022 | 262 | 3 | LTE
48116824 | 51.083679, 13.727417 | 21634 | 262 | 7 | UMTS
48139428 | 51.083679, 13.727417 | 21634 | 262 | 7 | UMTS
79305830 | 51.083679, 13.727417 | 1350 | 262 | 2 | UMTS
7758593 | 51.083679, 13.727417 | 21634 | 262 | 7 | UMTS
134564141 | 51.083679, 13.727417 | 21634 | 262 | 7 | UMTS
200085 | 51.083679, 13.727417 | 35221 | 262 | 1 | UMTS
```
87 changes: 87 additions & 0 deletions area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/python3
from math import cos, sqrt, pi
import argparse
import requests
import json

# Static
PREFIX = "https://opencellid.org/cell/getInArea?key="
PARAM = "&format=json"

# Class that represents a coordinate
class Coord:
def __init__(self, lat=0.0, lon=0.0):
self.lat = lat
self.lon = lon

# Constructor to parse coordinates from string in format: 'lat,lon'
@classmethod
def from_str(self, s):
try:
lat, lon = map(float, s.split(','))
return self(lat, lon)
except ValueError:
print("Error: could not parse location, quitting.")
quit()

# Takes number of decimal numbers, default 5
# Returns Cooridnate as str in format: lat,lon
def to_str(self, d = 5):
return str(round(self.lat, d)) + ',' + str(round(self.lon, d))

# Takes a area size in km^2.
# Returns coordiantes of the corners of the sqare with given area,
# surrounding the center
def square_from_point(self, area = 1.0):
c = 111.3 # km/°
a = sqrt(area) / 2 # km

lat_del = a / c
lon_del = a / (c * cos(self.lat * (pi / 180)))

p_max = Coord(self.lat + lat_del, self.lon + lon_del)
p_min = Coord(self.lat - lat_del, self.lon - lon_del)
return (p_max, p_min)


def main():
parser = argparse.ArgumentParser(prog='area')
parser.add_argument('-p', '--position', type=str, required=True,
help="Center position of the area. Format: lat,lon as floats")
parser.add_argument('-k', '--key', type=str, required=True,
help="Your apikey")
parser.add_argument('-a', '--area', type=float, default=0.1,
help="Size of the are in km²")


args = parser.parse_args()
loc = Coord.from_str(args.position)
p_max, p_min = loc.square_from_point(args.area)

# api request
url = PREFIX + args.key + "&BBOX=" + p_min.to_str() + ',' + p_max.to_str() + PARAM
api_result = requests.get(url)
api_result = json.loads(api_result.text)

#if api_result['error']:
# print("Error while reading opencellid API:\n{}".format(api_result['error']))
# quit()

# pretty printing
print(str(api_result['count']) + ' Stations Found')
print("{:^10}|{:^10}, {:^10}|{:^7}|{:^5}|{:^3}| radio".format(
"cellid", "lat", "lon", "lac", "mcc", "mnc"))
print("{:->11}{:->23}{:->8}{:->6}{:->4}------".format( "+","+","+","+","+"))

for cell in api_result['cells']:
print("{cid:9d} | {lat:8f}, {lon:8f} | {lac:5d} | {mcc:3d} | {mnc:d} | {radio}".format(
cid=cell['cellid'],
lat=cell['lat'],
lon=cell['lon'],
lac=cell['lac'],
mcc=cell['mcc'],
mnc=cell['mnc'],
radio=cell['radio']))

if __name__ == "__main__":
main()

0 comments on commit 94fd89a

Please sign in to comment.