def handle(self, *args, **options): file = 'moscow_bus_stations.csv' with open(os.path.join(settings.BASE_DIR, file), 'r', encoding='cp1251') as csvfile: reader = csv.reader(csvfile, delimiter=';') next(reader) # count = 0 for line in reader: # count += 1 station = Station() station.name = line[1] station.longitude = float(line[2]) station.latitude = float(line[3]) station.save() for route in line[7].split('; '): try: new_route = Route.objects.get(name=route) except Route.DoesNotExist: new_route = Route.objects.create(name=route) station.routes.add(new_route) print(f'доабвлена станция {station.name}') print( f'latitude {station.latitude} : longitude {station.longitude}' ) print(f'Всего доабавлено станций: {Station.objects.count()}') print(f'Всего добоавлено маршрутов: {Route.objects.count()}')
def getStations(): if request.args.get('lat') == None or request.args.get( 'lng') == None or request.args.get('dst') == None: abort(400) # get query string params lat = float(request.args.get('lat')) lng = float(request.args.get('lng')) dst = float(request.args.get('dst')) places_id = list() stations = list() places_tree = ET.parse( '/home/gasway/Gasway-WebService-Python-Flask-/app/static/places.xml') places = places_tree.getroot() for place in places: data = place.find('location') #for data in place.findall('location'): x = float(data.find('x').text) y = float(data.find('y').text) formula = (6371 * acos( cos(radians(y)) * cos(radians(lat)) * cos(radians(lng) - radians(x)) + sin(radians(y)) * sin(radians(lat)))) if (formula < dst): s = Station() s.place_id = place.get('place_id') s.name = place.find('name').text s.brand = place.find('brand').text s.cre_id = place.find('cre_id').text s.category = place.find('category').text s.address = data.find('address_street').text s.lat = y s.lng = x stations.append(s) places_id.append(place.attrib) prices_tree = ET.parse( '/home/gasway/Gasway-WebService-Python-Flask-/app/static/prices.xml') prices = prices_tree.getroot() for price in prices: if price.attrib in places_id: s_i = places_id.index(price.attrib) for p in price.findall('gas_price'): po = Price() po.type = p.get('type') po.price = p.text stations[s_i].prices.append(po) response = app.response_class(response=jsonpickle.encode( stations, unpicklable=False), status=200, mimetype='application/json') return response
def handle(self, *args, **options): with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile: reader = csv.DictReader(csvfile, delimiter=';') for row in reader: station = Station() station.id = row['ID'] station.name = row['Name'] station.latitude = row['Latitude_WGS84'] station.longitude = row['Longitude_WGS84'] station.save() for route in row['RouteNumbers'].split('; '): obj, created = Route.objects.get_or_create(name=route) station.routes.add(obj) print('All done')
def handle(self, *args, **options): with open('moscow_bus_stations.csv', 'r') as csvfile: csv_reader = csv.DictReader(csvfile, delimiter=';') for line in csv_reader: station = Station() station.latitude = line["Latitude_WGS84"] station.longitude = line["Longitude_WGS84"] station.name = line["Name"] string = line["RouteNumbers"].split(";") station.save() for i in range(len(string)): route = Route() route.name = string[i].replace(" ", "") route.save() station.routes.add(route) station.save() del string
def handle(self, *args, **options): with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile: stations_reader = csv.DictReader(csvfile, delimiter=';') for row in tqdm(stations_reader): station = Station() station.name = row['Name'] station.longitude = row['Longitude_WGS84'] station.latitude = row['Latitude_WGS84'] station.save() routes = [route for route in row['RouteNumbers'].split('; ')] for route in routes: object = Route.objects.filter(name=route).first() if object: station.routes.add(object) else: route = Route(name=route) route.save() station.routes.add(route) station.save()
import csv import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") django.setup() from app.models import Route, Station with open('moscow_bus_stations.csv', newline='', encoding='cp1251') as csvfile: reader = csv.DictReader(csvfile, delimiter=';') for row in reader: station = Station() station.id = row['ID'] station.name = row['Name'] station.latitude = row['Latitude_WGS84'] station.longitude = row['Longitude_WGS84'] station.save() for route in row['RouteNumbers'].split('; '): obj, created = Route.objects.get_or_create(name=route) station.routes.add(obj) print('All done')