Upload data.py
Browse files
data.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Copyright 2024 Ashok Kumar
|
| 2 |
+
|
| 3 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
you may not use this file except in compliance with the License.
|
| 5 |
+
You may obtain a copy of the License at
|
| 6 |
+
|
| 7 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
|
| 9 |
+
Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
See the License for the specific language governing permissions and
|
| 13 |
+
limitations under the License.'''
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
import requests
|
| 17 |
+
import json
|
| 18 |
+
import pandas as pd
|
| 19 |
+
import geopandas as gpd
|
| 20 |
+
import contextily as ctx
|
| 21 |
+
import tzlocal
|
| 22 |
+
import pytz
|
| 23 |
+
from PIL import Image
|
| 24 |
+
from datetime import datetime
|
| 25 |
+
import matplotlib.pyplot as plt
|
| 26 |
+
from geopy.exc import GeocoderTimedOut
|
| 27 |
+
from geopy.geocoders import Nominatim
|
| 28 |
+
import warnings
|
| 29 |
+
warnings.filterwarnings('ignore')
|
| 30 |
+
from plotly.graph_objs import Marker
|
| 31 |
+
import plotly.express as px
|
| 32 |
+
|
| 33 |
+
def flight_data(flight_view_level, country, local_time_zone, flight_info, airport):
|
| 34 |
+
geolocator = Nominatim(user_agent="flight_tracker")
|
| 35 |
+
loc = geolocator.geocode(country)
|
| 36 |
+
loc_box = loc[1]
|
| 37 |
+
extend_left =+12*flight_view_level
|
| 38 |
+
extend_right =+10*flight_view_level
|
| 39 |
+
extend_top =+10*flight_view_level
|
| 40 |
+
extend_bottom =+ 18*flight_view_level
|
| 41 |
+
lat_min, lat_max = (loc_box[0] - extend_left), loc_box[0]+extend_right
|
| 42 |
+
lon_min, lon_max = (loc_box[1] - extend_bottom), loc_box[1]+extend_top
|
| 43 |
+
|
| 44 |
+
tile_zoom = 8 # zoom of the map loaded by contextily
|
| 45 |
+
figsize = (15, 15)
|
| 46 |
+
columns = ["icao24","callsign","origin_country","time_position","last_contact","longitude","latitude",
|
| 47 |
+
"baro_altitude","on_ground","velocity","true_track","vertical_rate","sensors","geo_altitude",
|
| 48 |
+
"squawk","spi","position_source",]
|
| 49 |
+
data_url = "https://raw.githubusercontent.com/ashok2216-A/ashok_airport-data/main/data/airports.dat"
|
| 50 |
+
column_names = ["Airport ID", "Name", "City", "Country", "IATA/FAA", "ICAO", "Latitude", "Longitude",
|
| 51 |
+
"Altitude", "Timezone", "DST", "Tz database time zone", "Type", "Source"]
|
| 52 |
+
airport_df = pd.read_csv(data_url, header=None, names=column_names)
|
| 53 |
+
airport_locations = airport_df[["Name", "City", "Country", "IATA/FAA", "Latitude", "Longitude"]]
|
| 54 |
+
airport_country_loc = airport_locations[airport_locations['Country'] == str(loc)]
|
| 55 |
+
airport_country_loc = airport_country_loc[(airport_country_loc['Country'] == str(loc)) & (airport_country_loc['Latitude'] >= lat_min) &
|
| 56 |
+
(airport_country_loc['Latitude'] <= lat_max) & (airport_country_loc['Longitude'] >= lon_min) &
|
| 57 |
+
(airport_country_loc['Longitude'] <= lon_max)]
|
| 58 |
+
|
| 59 |
+
url_data = (
|
| 60 |
+
f"https://@opensky-network.org/api/states/all?"
|
| 61 |
+
f"lamin={str(lat_min)}"
|
| 62 |
+
f"&lomin={str(lon_min)}"
|
| 63 |
+
f"&lamax={str(lat_max)}"
|
| 64 |
+
f"&lomax={str(lon_max)}")
|
| 65 |
+
json_dict = requests.get(url_data).json()
|
| 66 |
+
|
| 67 |
+
unix_timestamp = int(json_dict["time"])
|
| 68 |
+
local_timezone = pytz.timezone(local_time_zone) # get pytz timezone
|
| 69 |
+
local_time = datetime.fromtimestamp(unix_timestamp, local_timezone).strftime('%Y-%m-%d %H:%M:%S')
|
| 70 |
+
time = []
|
| 71 |
+
for i in range(len(json_dict['states'])):
|
| 72 |
+
time.append(local_time)
|
| 73 |
+
df_time = pd.DataFrame(time,columns=['time'])
|
| 74 |
+
state_df = pd.DataFrame(json_dict["states"],columns=columns)
|
| 75 |
+
state_df['time'] = df_time
|
| 76 |
+
gdf = gpd.GeoDataFrame(
|
| 77 |
+
state_df,
|
| 78 |
+
geometry=gpd.points_from_xy(state_df.longitude, state_df.latitude),
|
| 79 |
+
crs={"init": "epsg:4326"}, # WGS84
|
| 80 |
+
)
|
| 81 |
+
return gdf
|
| 82 |
+
# geo_df = flight_tracking(flight_view_level = 6, country= 'India', local_time_zone='Asia/Kolkata', flight_info='baro_altitude', airport=1)
|