def run_city(verbose=True, step=10, begin=0, end=0): import csv filename = os.path.join(settings.BASE_PATH, 'apps', 'world', 'data', 'cities.csv') reader = csv.reader(open(filename, 'rb'), delimiter=',', quotechar="'") for row in reader: c = City() c.name = row[4] c.capital = bool(int(row[9])) c.world_city = bool(int(row[12])) c.mega_city = bool(int(row[13])) c.point = Point(float(row[22]), float(row[21])) #don't save the city if a country can't be found for it try: c.country = CountryBorder.objects.get(iso3=row[17]) except : continue try: c.state = StateBorder.objects.get(name=row[18]) except : try: c.state = StateBorder.objects.get(mpoly__contains=c.point) except : pass c.sqkm = int(row[40]) topleft = Point(float(row[48]), float(row[55])) topright = Point(float(row[51]), float(row[55])) bottomleft = Point(float(row[48]), float(row[52])) bottomright = Point(float(row[51]), float(row[52])) c.mpoly = MultiPolygon(Polygon(LinearRing(topleft, topright, bottomright, bottomleft, topleft))) c.save() print c.name
def postCity(): try: city = City() if "name" in request.form: city.name = request.form["name"] if "maxLat" in request.form: city.pack = request.form["maxLat"] if "maxLon" in request.form: city.pack = request.form["maxLon"] if "minLat" in request.form: city.pack = request.form["minLat"] if "minLon" in request.form: city.pack = request.form["minLon"] db.session.add(city) db.session.commit() return make_response(jsonify( { 'ok': 'city created' } ), 200) except: return make_response(jsonify( { 'error': 'Server Error' } ), 500)
def create_or_getCity(cityName, state, lat,lon): try: c = City.objects.get(name=cityName) except City.DoesNotExist: r = requests.get("http://photon.komoot.de/api/?q={},{}&limit=1&lat={}&lon={}".format(cityName, state.name, lat,lon)).json() lastCity = City.objects.latest('created_on') c = City() c.id = lastCity.id +1 c.name=cityName c.stateID=state.id c.lat= r['features'][0]['geometry']['coordinates'][1] c.lon= r['features'][0]['geometry']['coordinates'][0] c.live="true" c.save() return c
def post_cities(id): ''' Post a new city ''' try: content = request.get_json() except: return (jsonify({"error": "Not a JSON"}), 400) name = content.get("name") if name is None: return (jsonify({"error": "Missing name"}), 400) my_state = storage.get('State', id) if my_state is None: abort(404) new_city = City() new_city.state_id = id new_city.name = name new_city.save() return (jsonify(new_city.to_dict()), 201)
def seed_cities(self, number): """ Crée des enregistrements aléatoires dans la table des villes :param number: nombre de villes :return: void """ for i in range(0, number): city = City() city.name = self.fake.city() city.country = self.fake.country() city.is_capital = self.fake.pybool() self.city_service.save(city) capitals = self.city_service.all_capitals() non_capitals = self.city_service.all_non_capitals() for city in non_capitals: city.capital_id = random.choice(capitals).id self.city_service.save(city)
def post(self): city = None if self.request.get("city").lower() != "": if not getCityObjectByName(self.request.get("city").lower()): city = City() city.name = self.request.get("city").lower() city.put() else: city = getCityObjectByName(self.request.get("city").lower()) bin = None if not getBinObjectByName(city.key, self.request.get("bin_name").lower()): bin = Bin(parent=city.key) bin.name = self.request.get("bin_name").lower() bin.image = self.request.get("image").lower() bin.sortingInstructions = self.request.get( "sorting_instructions") putBinObject(bin) else: pass bin_add_template = jinja_env.get_template("templates/addbin.html") self.response.out.write(bin_add_template.render())
def add_city(self): city = City() city.name = self.info['name'] city.state_name = self.info['state_name'] return self.save(city)