Esempio n. 1
0
    def run(self, filepath=None):
        addresses = {
            'rob': {
                'name': 'First test Location',
                'phone': '250-588-9595',
                'address': '3329 University Woods, Canada',
                'price': 12.35,
            },
            'rob2': {
                'name': 'Second test Location',
                'phone': '800 647 5463',
                'address': '3800 Finnerty Rd,  Canada',
                'price': 22.42,
            },
            'rob3': {
                'name': 'Third test Location',
                'phone': '202-426-6841',
                'address': '3440 Saanich Rd,  Canada',
                'price': 11.52,
            },
            'rob4': {
                'name': 'Fourth test Location',
                'phone': '202.334.6000',
                'address': '1150 Douglas St, Victoria, V8W3M9, Canada',
                'price': 16.65,
            },
            'rob5': {
                'name': 'Fifth test Location',
                'phone': '240-283-9942',
                'address': '3190 Shelbourne St, Victoria, V8T3A8, Canada',
                'price': 6.99,
            }
        }
        for username, address in addresses.items():
            address_entry = AddressEntry()
            address_entry.user_id = User.query.filter(
                User.username == username).first().id
            address_entry.name = address['name']
            address_entry.phone = address['phone']

            geocoder = mapbox.Geocoder()
            selected_feature = json.loads(
                geocoder.forward(
                    address['address']).content.decode('utf-8'))['features'][0]
            address_entry.longitude = selected_feature['center'][0]
            address_entry.latitude = selected_feature['center'][1]
            address_entry.street_number = selected_feature['address']
            address_entry.street_name = selected_feature['text']
            address_entry.mapbox_place_name = selected_feature['place_name']
            for feature in selected_feature['context']:
                if feature['id'].startswith('place'):
                    address_entry.county = feature['text']

                if feature['id'].startswith('postcode'):
                    address_entry.postal_code = feature['text']

                if feature['id'].startswith('region'):
                    address_entry.state = feature['text']

                if feature['id'].startswith('country'):
                    address_entry.country = feature['text']

                if feature['id'].startswith('neighborhood'):
                    address_entry.neighborhood = feature['text']
            db_session = db.session()
            db_session.add(address_entry)
            db_session.commit()
            print("Created: %s" % address['name'])
Esempio n. 2
0
    def post(self, action):
        spot = json.loads(request.data)
        address_entry = AddressEntry()

        address_entry.user_id = g.user_id
        address_entry.is_dest = False
        address_entry.is_avail = True
        address_entry.price = spot['price']

        address_entry.photo_url = spot['photo_url']
        address_entry.spot_type = spot['spot_type']
        address_entry.avail_type = spot['availability_type']

        # update the user's phone number
        # spot['phone']
        
        geocoder = mapbox.Geocoder()
        features = json.loads(geocoder.forward(spot['address']).content.decode('utf-8'))['features']

        selected_feature = None
        for feature in features:
            if 'address' in feature:
                selected_feature = feature
                break
        if not selected_feature:
            return 'The is no such place on mapbox!', 400

        address_entry.longitude = selected_feature['center'][0]
        address_entry.latitude = selected_feature['center'][1]
        address_entry.street_number = selected_feature['address']
        address_entry.street_name = selected_feature['text']
        address_entry.mapbox_place_name = selected_feature['place_name']

        # address_entry.name = data.get('name')
        # Generate a dict from
        for feature in selected_feature['context']:
            if feature['id'].startswith('place'):
                address_entry.county = feature['text']

            if feature['id'].startswith('postcode'):
                address_entry.postal_code = feature['text']

            if feature['id'].startswith('region'):
                address_entry.state = feature['text']

            if feature['id'].startswith('country'):
                address_entry.country = feature['text']

            if feature['id'].startswith('neighborhood'):
                address_entry.neighborhood = feature['text']

        db_session = db.session()
        db_session.add(address_entry)
        db_session.commit()

        origin = {
            'type': 'Feature',
            'properties': {'name': address_entry.street_name},
            'geometry': {
                'type': 'Point',
                'coordinates': [float(address_entry.longitude), float(address_entry.latitude)]
            }
        }

        # just for the destinations within the threshold (20Km), create the distance from the new spot
        for sub_address in AddressEntry.query.filter(AddressEntry.is_dest==True, AddressEntry.is_avail==True):
            distance = get_straight_distance(float(address_entry.latitude), float(address_entry.longitude), float(sub_address.latitude), float(sub_address.longitude))
            if distance > 20000:
                continue

            address_distance = AddressDistance()

            destination = {
                'type': 'Feature',
                'properties': {'name': sub_address.street_name},
                'geometry': {
                    'type': 'Point',
                    'coordinates': [float(sub_address.longitude), float(sub_address.latitude)],
                }
            }

            content = mapbox.Directions().directions([origin, destination],'mapbox.driving').geojson()
            address_distance.address_a_id = sub_address.id      # destination
            address_distance.address_b_id = address_entry.id    # parking spot
            address_distance.mapbox_response = json.dumps(content)
            address_distance.speed_scala = sum([feature['properties']['distance'] for feature in content['features']])
            db_session = db.session()
            db_session.add(address_distance)
            db_session.commit()
        return 'success'
Esempio n. 3
0
def _update_users_address():
    # Frame Two Logic
    address_entry_form = AddressEntryForm()
    if address_entry_form.validate_on_submit():
        # Join the address fields into a single field.
        # 'address city state postal_code'
        lookup = ','.join([address_entry_form.data['address'],
            address_entry_form.data['city'],
            address_entry_form.data['state'],
            address_entry_form.data['postal_code']])

        geocoder = mapbox.Geocoder()
        features = json.loads(geocoder.forward(lookup).content.decode('utf-8'))['features']
        features = [ feature for feature in features if ('address' in feature) ]
        # Storage Convenience
        session['_phone'] = address_entry_form.data.get('phone', None)
        session['_building_name'] = address_entry_form.data.get('building_name', None)
        if address_entry_form.data.get('form_state', None) == 'validation':
            # Store the features in the session based off feature['id'].
            session['_geocoded_values'] = json.dumps({feature['id']:feature for feature in features })
            return jsonify({'features': features})
        else:
            abort(400) # Probably trying to do something nasty.

        address_entry_form = None

    # Frame Three Logic
    address_entry_commit_form = AddressEntryCommitForm()
    if address_entry_commit_form.validate_on_submit():
        query_set = AddressEntry.query.filter(AddressEntry.user_id==current_user.id)
        if query_set.count() == 0:
            address_entry = AddressEntry()
        else:
            address_entry = query_set.first()

        if address_entry_commit_form.data.get('form_state', None) == 'commit':
            address_entry.user_id = current_user.id
            address_entry.is_dest = True
            address_entry.is_avail = True

            address_entry.name = session['_building_name']
            address_entry.phone = session['_phone']
            del session['_building_name'] # Cleanup the session
            del session['_phone'] # Cleanup the session
            # https://www.mapbox.com/help/define-lat-lon/
            # Sometimes mapbox returns long/lat or lat/long. With the Geocoding API, it seems
            #  long/lat is the format.
            # With that, make sure you double check the projections. Google will return a long in the
            #  positive specturm while mapbox will return a long in the negative spectrum. Not a problem technically,
            #  but this very question breeds hours of googling to understand the contextual differences between map
            #  providers.
            #  https://en.wikipedia.org/wiki/Map_projection
            #  https://www.mapbox.com/tilemill/docs/manual/basics/
            #  There is even some drama with the Google projections. epsg3857 and 900913 should be the same thing;
            #   but are very different when it comes to the specification. It's fun to read about. :)
            #  http://gis.stackexchange.com/questions/50347/which-is-correct-projection-in-arcgis-server-for-epsg3857-900913
            selected_feature = json.loads(session['_geocoded_values'])[address_entry_commit_form.data['picked']]
            del session['_geocoded_values'] # Clean up the session
            address_entry.longitude = selected_feature['center'][0]
            address_entry.latitude = selected_feature['center'][1]
            address_entry.street_number = selected_feature['address']
            address_entry.street_name = selected_feature['text']
            address_entry.mapbox_place_name = selected_feature['place_name']

            # address_entry.name = data.get('name')
            # Generate a dict from
            for feature in selected_feature['context']:
                if feature['id'].startswith('place'):
                    address_entry.county = feature['text']

                if feature['id'].startswith('postcode'):
                    address_entry.postal_code = feature['text']

                if feature['id'].startswith('region'):
                    address_entry.state = feature['text']

                if feature['id'].startswith('country'):
                    address_entry.country = feature['text']

                if feature['id'].startswith('neighborhood'):
                    address_entry.neighborhood = feature['text']

            db_session = db.session()
            db_session.add(address_entry)
            db_session.commit()

            origin = {
                'type': 'Feature',
                'properties': {'name': address_entry.name},
                'geometry': {
                    'type': 'Point',
                    'coordinates': [float(address_entry.longitude), float(address_entry.latitude)]
                }
            }

            # just for the parking spots within the threshold (20Km), create/update the distance from the destination
            for sub_address in AddressEntry.query.filter(AddressEntry.is_dest==False, AddressEntry.is_avail==True):
                distance = get_straight_distance(float(address_entry.latitude), float(address_entry.longitude), float(sub_address.latitude), float(sub_address.longitude))
                if distance > 20000:
                    continue

                query_set = AddressDistance.query.filter(AddressDistance.address_a_id==address_entry.id, AddressDistance.address_b_id==sub_address.id)
                if query_set.count() == 0:
                    address_distance = AddressDistance()
                else:
                    address_distance = query_set.first()

                destination = {
                    'type': 'Feature',
                    'properties': {'name': sub_address.name},
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [float(sub_address.longitude), float(sub_address.latitude)],
                    }
                }

                content = mapbox.Directions().directions([origin, destination],'mapbox.driving').geojson()
                address_distance.address_a_id = address_entry.id
                address_distance.address_b_id = sub_address.id
                address_distance.mapbox_response = json.dumps(content)
                address_distance.speed_scala = sum([feature['properties']['distance'] for feature in content['features']])
                db_session = db.session()
                db_session.add(address_distance)
                db_session.commit()

    return jsonify({
        'waypoints': AddressDistance.get_direction_pois(address_entry.id),
        'origin': address_entry.as_geojson(),
    })
Esempio n. 4
0
    def post(self):
        lookup = json.loads(request.data)['address']
        geocoder = mapbox.Geocoder()
        features = json.loads(geocoder.forward(lookup).content.decode('utf-8'))['features']

        selected_feature = None
        for feature in features:
            if 'address' in feature:
                selected_feature = feature
                break
        if not selected_feature:
            return 'The is no such place on mapbox!', 400

        # print lookup
        # print selected_feature['place_name'], '@@@@@@@@'    
        # print selected_feature['relevance']

        query_set = AddressEntry.query.filter(AddressEntry.user_id==g.user_id, AddressEntry.is_dest==True, AddressEntry.is_avail==True)
        if query_set.count() == 0:
            address_entry = AddressEntry()
            address_entry.user_id = g.user_id
            address_entry.is_dest = True
            address_entry.is_avail = True
        else:
            address_entry = query_set.first()


        address_entry.name = json.loads(request.data)['spot_name']
        address_entry.longitude = selected_feature['center'][0]
        address_entry.latitude = selected_feature['center'][1]
        address_entry.street_number = selected_feature['address']
        address_entry.street_name = selected_feature['text']
        address_entry.mapbox_place_name = selected_feature['place_name']

        # Generate a dict from
        for feature in selected_feature['context']:
            if feature['id'].startswith('place'):
                address_entry.county = feature['text']

            if feature['id'].startswith('postcode'):
                address_entry.postal_code = feature['text']

            if feature['id'].startswith('region'):
                address_entry.state = feature['text']

            if feature['id'].startswith('country'):
                address_entry.country = feature['text']

            if feature['id'].startswith('neighborhood'):
                address_entry.neighborhood = feature['text']

        db_session = db.session()
        db_session.add(address_entry)
        db_session.commit()

        origin = {
            'type': 'Feature',
            'properties': {'name': address_entry.name},
            'geometry': {
                'type': 'Point',
                'coordinates': [float(address_entry.longitude), float(address_entry.latitude)]
            }
        }

        # just for the parking spots within the threshold (20Km), create/update the distance from the destination
        for sub_address in AddressEntry.query.filter(AddressEntry.is_dest==False, AddressEntry.is_avail==True):
            distance = get_straight_distance(float(address_entry.latitude), float(address_entry.longitude), float(sub_address.latitude), float(sub_address.longitude))
            if distance > 20000:
                continue

            query_set = AddressDistance.query.filter(AddressDistance.address_a_id==address_entry.id, AddressDistance.address_b_id==sub_address.id)
            if query_set.count() == 0:
                address_distance = AddressDistance()
            else:
                address_distance = query_set.first()

            destination = {
                'type': 'Feature',
                'properties': {'name': sub_address.name},
                'geometry': {
                    'type': 'Point',
                    'coordinates': [float(sub_address.longitude), float(sub_address.latitude)],
                }
            }

            content = mapbox.Directions().directions([origin, destination],'mapbox.driving').geojson()
            address_distance.address_a_id = address_entry.id
            address_distance.address_b_id = sub_address.id
            address_distance.mapbox_response = json.dumps(content)
            address_distance.speed_scala = sum([feature['properties']['distance'] for feature in content['features']])
            db_session = db.session()
            db_session.add(address_distance)
            db_session.commit()

        return  {
                    'waypoints': AddressDistance.get_direction_pois(address_entry.id),
                    'origin': address_entry.as_geojson(True),
                }
Esempio n. 5
0
def sell_parking():
    if request.method == 'GET':
        spots = AddressEntry.query.filter(AddressEntry.user_id==current_user.id, AddressEntry.is_dest==False)
        return render_template('users/sell_parking.html', spots=spots)
    else:
        address_entry_form = AddressEntryForm()
        if address_entry_form.validate_on_submit():
            # Join the address fields into a single field.
            # 'address city state postal_code'
            lookup = ' '.join([address_entry_form.data['address'],
                address_entry_form.data['city'],
                address_entry_form.data['state']])

            geocoder = mapbox.Geocoder()
            features = json.loads(geocoder.forward(lookup).content.decode('utf-8'))['features']
            features = [ feature for feature in features if ('address' in feature) ]
            # Storage Convenience
            session['_price'] = address_entry_form.data.get('price', None)
            if address_entry_form.data.get('form_state', None) == 'validation':
                # Store the features in the session based off feature['id'].
                session['_geocoded_values'] = json.dumps({feature['id']:feature for feature in features })
                return jsonify({'features': features})
            else:
                abort(400) # Probably trying to do something nasty.

            address_entry_form = None

        # Frame Three Logic
        address_entry_commit_form = AddressEntryCommitForm()
        if address_entry_commit_form.validate_on_submit():
            address_entry = AddressEntry()

            if address_entry_commit_form.data.get('form_state', None) == 'commit':
                address_entry.user_id = current_user.id
                address_entry.is_dest = False
                address_entry.is_avail = True

                address_entry.price = session['_price']
                del session['_price']

                selected_feature = json.loads(session['_geocoded_values'])[address_entry_commit_form.data['picked']]
                del session['_geocoded_values'] # Clean up the session
                address_entry.longitude = selected_feature['center'][0]
                address_entry.latitude = selected_feature['center'][1]
                address_entry.street_number = selected_feature['address']
                address_entry.street_name = selected_feature['text']
                address_entry.mapbox_place_name = selected_feature['place_name']

                # address_entry.name = data.get('name')
                # Generate a dict from
                for feature in selected_feature['context']:
                    if feature['id'].startswith('place'):
                        address_entry.county = feature['text']

                    if feature['id'].startswith('postcode'):
                        address_entry.postal_code = feature['text']

                    if feature['id'].startswith('region'):
                        address_entry.state = feature['text']

                    if feature['id'].startswith('country'):
                        address_entry.country = feature['text']

                    if feature['id'].startswith('neighborhood'):
                        address_entry.neighborhood = feature['text']

                db_session = db.session()
                db_session.add(address_entry)
                db_session.commit()

                origin = {
                    'type': 'Feature',
                    'properties': {'name': address_entry.street_name},
                    'geometry': {
                        'type': 'Point',
                        'coordinates': [float(address_entry.longitude), float(address_entry.latitude)]
                    }
                }

                # just for the destinations within the threshold (20Km), create the distance from the new spot
                for sub_address in AddressEntry.query.filter(AddressEntry.is_dest==True, AddressEntry.is_avail==True):
                    distance = get_straight_distance(float(address_entry.latitude), float(address_entry.longitude), float(sub_address.latitude), float(sub_address.longitude))
                    if distance > 20000:
                        continue

                    address_distance = AddressDistance()

                    destination = {
                        'type': 'Feature',
                        'properties': {'name': sub_address.street_name},
                        'geometry': {
                            'type': 'Point',
                            'coordinates': [float(sub_address.longitude), float(sub_address.latitude)],
                        }
                    }

                    content = mapbox.Directions().directions([origin, destination],'mapbox.driving').geojson()
                    address_distance.address_a_id = sub_address.id      # destination
                    address_distance.address_b_id = address_entry.id    # parking spot
                    address_distance.mapbox_response = json.dumps(content)
                    address_distance.speed_scala = sum([feature['properties']['distance'] for feature in content['features']])
                    db_session = db.session()
                    db_session.add(address_distance)
                    db_session.commit()
                return 'success'