Belarussian law restricts drone flights on some zones: important governmental factories, military bases, and borders. These zones defined in http://ais.mil.by/doc/Ustanovlenie_zon.pdf document but it is not very usable format. The Belarussian government provided service http://map.nca.by/map.html, but this service very unstable and works strange sometimes. And in these cases, drone pilots need a more stable resource for control flight restrictions. As a solution, I propose to use a telegram bot. In this messenger, the user can easily share his location with the bot and get a response: is flight a legal in a provided location or not. As a bonus, for received point bot can provide additional information, such as weather conditions.
Initial data preparations
As a source of data for this but I prefer to use a document, provided by Ministry of defense. But in this decree data presented as not easily readable for computers format. For resolve this issue I copied data from all tables into a txt file and parsed it. See the example of the file below.
9 54°18'12''N 029°14'53''E 2,5 10 53°18'41''N 028°47'54''E 2,5 11 53°10'17''N 027°37'42''E 3 12 53°56'05"N 027°40'44"E 53°55'16"N 027°45'09"E 53°53'12"N 027°44'03"E 53°54'01"N 027°39'38"E 53°56'05"N 027°40'44"E 13 55°09'02"N 026°47'53"E
In the provided example you can see that file contains points in different formats: some areas presented as circles with center which has coordinates in WGS-84 format and radius in kilometers, some areas presented as polygons which have only world geo system coordinates for each point. So, the application should support all of these types.
Another issue with this data is the format of points: WGS-84 differs with the format in which Telegram represents a geographical coordinate, it has a minutes and seconds section. Good news is this format is just another representation of floating part of Telegram format. How to convert WGS-84 to regular lat-lng format code provided below:
def wgs_to_mercator(wgs_coordinate): wgs_coordinate = wgs_coordinate.replace("''", '"') # fix string, because some string contains '' format for seconds degrees, minutes, seconds = extract_wgs_parts(wgs_coordinate) return degrees + minutes / 60 + seconds / 3600
After it, I can save it into JSON. If you need, you can see the resulting JSON.
Creating the bot
I hope you now that if you want to create a new bot, you need to write about it to Telegram’s Bot Father and he will give you API-key. For use this bot from python code, I prefer to use the python-telegram-bot library. This library is easy-to-use a has a lot of documentation and examples for many common use-cases. Please, read the documentation for this library to understand how to set up and use it. For my case, the most important thing is handling messages with a location.
For handle this type of messages I need to describe filter for MessageHandler – it will process only location messages.
The main part of the application – resolve is flights are legal in this location or not. For solving it, we need to separate circle and polygon areas. For work with polygon areas, I prefer to use shapely. This library allows developers to create polygons and points and calculate is point lies within a polygon. It’s simple:
For circle areas, a solution is more complex. Radiuses are provided in kilometers and coordinates are geodesics angles. For check is point lies within a circle we need to calculate geodesic distance and compare it with the restricted area radius. It’s simple if you will use geopy. This library has module distance which contains a lot of functions for calculating different kinds of distances, and of course, it can calculate geodesic distance. This library is also can used for work with Geocoding APIs.
For deploying the bot to the server I packed it into python egg with setup.py via
python setup.py sdist and deploy it to local pip via twine.
You can try to use this bot: https://t.me/can_i_fly_bot
For the future, I’ll try to refactor it and make it open-source.