Beispiel #1
0
def showResponse(jsonResponse):
    print "\n この場所はジオフェンスにあります"
    perm_geocoder = Geocoder()  # マップボックスの統合
    perm_geocoder.session.params['access_token'] = 'pk.eyJ1IjoiYnJhZHdib25uIiwiYSI6ImNqMjd3bmEwbjAwMjQyeHF0OGp3dm5ibWUifQ.uNds-BFopyeVQY7beRAeQw'
    for row in jsonResponse['rows']:
        try:
            response = perm_geocoder.reverse(
                lon=row['geometry']['coordinates'][0],
                lat=row['geometry']['coordinates'][1],
                types=['address','neighborhood','locality','place','region']
            )
        except:
            response = perm_geocoder.reverse(
                lon=row['geometry']['coordinates'][0][0][0],
                lat=row['geometry']['coordinates'][0][0][1],
                types=['address','neighborhood','locality','place','region']
            )
        try:
            address = response.geojson()['features'][0]['place_name']
        except:
            address = "場所は不明です"
        print " " + row['id'] + " " + address
        
    showMe = raw_input("\n JSONを見たいですか?(y/n) > ")
    if showMe == "y" or showMe == "Y":
        print "\n JSONレスポンス:"
        pprint(jsonResponse)
Beispiel #2
0
def fill_departments():
    geocoder = Geocoder(
        access_token=
        'pk.eyJ1IjoibWVoZGliYWhhIiwiYSI6ImNrOGZ3bWdmMDAya24zZm8xbGJkYWw3cXkifQ.fS6Ny7_0bZ7swBKW7rvgEQ'
    )

    with open('departments.json') as json_file:
        departments = json.load(json_file)

    with open('locations.csv', mode='r') as csv_file:
        locations = list(csv.DictReader(csv_file, delimiter=';'))

    for f in departments['features']:
        f['properties']['amount'] = 0

    for l in locations:
        response = geocoder.reverse(lon=l['longitude'],
                                    lat=l['latitude'],
                                    types=['region'],
                                    limit=1).geojson()
        dep_code = response['features'][0]['properties']['short_code'].split(
            '-')[1]
        match = None
        for f in departments['features']:
            if f['properties']['code'] == dep_code:
                f['properties']['amount'] += float(l['amount'])

    with open("5-france.json", "w") as write_file:
        json.dump(departments, write_file)
class GeoToolbox:
    _geocoder = None

    #constructor
    def __init__(self):
        self._geocoder = Geocoder(access_token = MAPBOX_TOKEN.MAPBOX_ACCESS_TOKEN)


    # get the address from lat and lon
    def getAddress(self, latitude, longitude):
        fullAnswer = {"status": False, "code" : None, "address": None}

        response = self._geocoder.reverse(lon = longitude, lat = latitude)

        fullAnswer["code"] = response.status_code

        if(response.status_code == 200):
            fullAnswer["status"] = True


            for f in response.geojson()['features']:
                fullAnswer["address"] = u'{place_name}'.format(**f)
                break


        return fullAnswer
def get_Postal_Address_Mapbox(longitude, latitude, api_key):
    endpoint_full = "mapbox.places-permanent"  # utilisé pour des fonctions avancées payantes (on n'utilise pas!)
    endpoint = "mapbox.places"
    geocoder = Geocoder(access_token=api_key)
    response = geocoder.reverse(lon=longitude, lat=latitude)
    first = response.geojson()['features'][0]
    if response.status_code == 200:
        return first['place_name']
    else:
        return response.status_code
Beispiel #5
0
def get_place(lat, lon):
    """Get the region name of the center lat/lon
    """
    geocoder = Geocoder()
    geocoder = Geocoder(access_token=os.environ['MapboxAccessToken'])
    place = 'Somewhere over the clouds!'
    try:
        response = geocoder.reverse(lon=lon, lat=lat, types=['region'])
        if response.status_code == 200:
            features = response.geojson()['features']
            place = features[0].get('place_name')
        return place
    except:
        return place
class GeoToolbox:
    _geocoder = None

    #constructor
    def __init__(self):
        self._geocoder = Geocoder(
            access_token=MAPBOX_TOKEN.MAPBOX_ACCESS_TOKEN)

    # get the address from lat and lon
    def getAddress(self, latitude, longitude):
        fullAnswer = {"status": False, "code": None, "address": None}

        response = self._geocoder.reverse(lon=longitude, lat=latitude)

        fullAnswer["code"] = response.status_code

        if (response.status_code == 200):
            fullAnswer["status"] = True

            for f in response.geojson()['features']:
                fullAnswer["address"] = u'{place_name}'.format(**f)
                break

        return fullAnswer
Beispiel #7
0
class EssentialsConverter:
    def __init__(self):
        # Public API. So commiting with code. No big secret here.

#         self.access_token = "pk.eyJ1IjoiYXNobWFwNGdkIiwiYSI6ImNrOXBjb2k2dDA5YW4zb24xb3A2cWs5YXYifQ.3qtCEWPKAOEftYEEUDfDSQ"
        self.access_token = "pk.eyJ1IjoiYXNocm9uYXZvbmEiLCJhIjoiY2thYjRtY2hmMDkyeDJ0bzg0cHF5dTh5diJ9.10oEwG3nFpYrhYy-LufSuA"
        self.coder = Geocoder(access_token=self.access_token)
        self.cityDict = {}
        self.cityList = []
        self.processedBatch = []
        self.failed = []
        self.failed_ids = []
        self.failed_cities = []
        self.gaussian = []
        self._api = 0

    @property
    def request_ctr(self):
        self._api += 1

    @property
    def rate_limit_exceeded(self):
        return self._api and not self._api % 600

    def populate(self, dir="./tmp/resources/", fromFile=False):
        if fromFile:
            try: 
                with open(dir + "cityData.json") as c_list:
                    data = json.load(c_list)
                with open(dir + "debug.json") as d_file:
                    debug = json.load(d_file)
            except FileNotFoundError:
                logger.error('Wanted to access cityData and debug, but not found')
                pass

            self.cityDict = data["cityboundaries"]
            self.cityList = data["cities"]

            self.gaussian = debug["gaussian"]
            self.failed_cities = debug["failed_cities"]
            self.failed_ids = debug["failed_ids"]
            self.failed = debug["failed_entries"]
        else:  # read in city list and city dicts from other sources like the google sheet
            logger.warning('CityData file not used')
            pass

    def generate_geojson(self, oldData=None):
        update = []
        if oldData:
            update = oldData["features"]
        update += self.processedBatch

        geojson = {
            "type": "FeatureCollection",
            "lastupdated": time.ctime(),
            "features": update,
        }

        return geojson
    def check_city(self, entry):
        city = " ".join((entry["city"], entry["state"]))

        if city not in self.cityList:
            self.make_boundaries(city)
            self.cityList.append(city)

    def make_boundaries(self, city):
        lvl1 = ["district"]
        lvl2 = ["place"]
        lvl3 = ["place", "locality", "neighborhood"]

        self.request_ctr
        response = self.coder.forward(city, types=lvl1, country=["in"], limit=1)

        if not response.json()["features"]:
            self.request_ctr
            response = self.coder.forward(city, types=lvl2, country=["in"], limit=1)

            # if not response.json()["features"]:
            #     self.request_ctr
            #     response = self.coder.forward(city, types=lvl2, country=["in"], limit=1)

            if not response.json()["features"]:
                self.request_ctr
                response = self.coder.forward(
                    city, types=lvl3, country=["in"], limit=1
                )

                if not response.json()["features"]:
                    self.request_ctr
                    response = self.coder.forward(city, country=["in"], limit=1)

        if not response.json()["features"]:
            self.failed_cities.append(city)
            return

        feat = response.json()["features"][0]
        city_center = feat["center"]

        if "bbox" in feat.keys():
            city_bbox = feat["bbox"]
        else:
            city_bbox = []

        self.cityDict[city] = {"bbox": city_bbox, "center": city_center}

        logger.info(f"Boundaries for {city} has been added successfully")

    @staticmethod
    def get_icon(category):
        if category == "Accommodation and Shelter Homes":
            return "homes"
        elif category == "Ambulance":
            return "ambulance"
        elif category == "Community Kitchen":
            return "kitchen"
        elif category == "CoVID-19 Testing Lab":
            return "lab"
        elif category == "Delivery [Vegetables, Fruits, Groceries, Medicines, etc.]":
            return "delivery"
        elif category == "Fire Brigade":
            return "fire"
        elif category == "Free Food":
            return "food"
        elif category == "Fundraisers":
            return "fund"
        elif category == "Government Helpline":
            return "helpline"
        elif category == "Hospitals and Centers":
            return "hospital"
        elif category == "Mental well being and Emotional Support":
            return "wellbeing"
        elif category == "Police":
            return "police"
        elif category == "Senior Citizen Support":
            return "seniors"
        elif category == "Transportation":
            return "transport"
        elif category == "Quarantine Facility":
            return "quarantine"
        elif category == "Other":
            return "other"

        else:
            return "unknown"

    @staticmethod
    def scrape_url(url, start=33, end=None):
        reg = url[start:end].split(",")

        geom = {"type": "Point", "coordinates": [reg[1], reg[0]]}
        return geom

    def make_feature(self, entry):
        url_format = "http://www.google.com/maps/place/"

        # Parse entry data
        i = entry["recordid"]
        name = entry["nameoftheorganisation"]
        desc = entry["descriptionandorserviceprovided"]
        category = entry["category"]
        state = entry["state"]
        phone = entry["phonenumber"]
        contact = entry["contact"]

        # Declare new props
        q_name = ""
        q_addr = ""
        geom = {}

        # Set flags
        icon = self.get_icon(entry["category"])
        isHealthcare = 0

        if category == "CoVID-19 Testing Lab" or category == "Hospitals and Centers":
            isHealthcare = 1

        city = " ".join((entry["city"], entry["state"]))
        query = ", ".join((entry["nameoftheorganisation"], entry["city"]))

        maps = False
        if url_format in contact:
            maps = True


        if "PAN" in city:
            if "India" in city:
                query = "India"
                state = "PAN India"
            else:
                query = entry["city"]
                state = " ".join(["PAN", state])
                logger.info("Pan entry saved as ", state)

        
        # Skipped entries

        if city not in self.cityDict:
            self.failed.append(entry)
            self.failed_ids.append(i)
            return

        c_bbox = self.cityDict[city]["bbox"]
        c_center = self.cityDict[city]["center"]


        if not maps:
            if c_bbox != []:
                self.request_ctr
                resp = self.coder.forward(query, country=["in"], bbox=c_bbox, limit=1)
            else:
                self.request_ctr
                resp = self.coder.forward(query, country=["in"], limit=1)

            target = resp.geojson()

            # Get data
            if target["features"]:  # condition -> non empty response
                geom = target["features"][0]["geometry"]
                q_name = target["features"][0]["text"]
                q_addr = target["features"][0]["place_name"]
            else:  # else -> empty response - use big brain trickery
                self.gaussian.append(i)
                if c_bbox:
                    sd = min(abs(c_bbox[0] - c_bbox[2]) / 8, abs(c_bbox[1] - c_bbox[3]) / 8)
                else:
                    sd = c_center[0] * 0.0004

                lng = gauss(c_center[0], sd)
                lat = gauss(c_center[1], sd)

                geom = {"type": "Point", "coordinates": [lng, lat]}
                q_addr = city
                q_name = ""
        
            
        if url_format in contact:
            geom = self.scrape_url(contact)
            self.request_ctr
            reverse = self.coder.reverse(geom["coordinates"][0], geom["coordinates"][1])
            target = reverse.geojson()

            if target["features"]:
                q_name = target["features"][0]["text"]
                q_addr = target["features"][0]["place_name"]

        if "PAN" in city:
            if "India" in city:
                q_addr = "India"
            else:
                q_addr = ", ".join([entry["state"], "India"])

        prop = {
            "recordid": i,
            "name": name,
            "desc": desc,
            "geoTag": q_name,
            "addr": q_addr,
            "state": state,
            "phone": phone,
            "contact": contact,
            "priority": isHealthcare,
            "icon": icon,
        }

        self.processedBatch.append(
            {"type": "Feature", "properties": prop, "geometry": geom}
        )

    def process_entry(self, entry):
        self.check_city(entry)  # Generates city data if its not been done yet.
        self.make_feature(entry)
        logger.info(f'Processed #{entry["recordid"]}')
Beispiel #8
0
def main():

    # ------
    # Static
    # ------
    
    service = Static()
    response = service.image('mapbox.satellite', lon=-61.7, lat=12.1, z=12)
    print(response.status_code)
    print(response.headers['Content-Type'])

    portland = {
        'type': 'Feature',
        'properties': {'name': 'Portland, OR'},
        'geometry': {
            'type': 'Point',
            'coordinates': [-122.7282, 45.5801]
        }
    }

    bend = {
        'type': 'Feature',
        'properties': {'name': 'Bend, OR'},
        'geometry': {
            'type': 'Point',
            'coordinates': [-121.3153, 44.0582]
        }
    }

    response = service.image('mapbox.satellite', features = [portland, bend])
    print(response.status_code)
    print(response.headers['Content-Type'])

    with open('map.png', 'wb') as f:
        _ = f.write(response.content)

    help(Static)

    # --------
    # Geocoder
    # --------

    perm_geocoder = Geocoder(name='mapbox.places-permanet')
    geocoder = Geocoder()

    # Limits

    response = geocoder.forward('Chester, NJ')
    print(response.headers['X-Rate-Limit-Interval'])
    print(response.headers['X-Rate-Limit-Limit'])
    print(response.headers['X-Rate-Limit-Reset'])

    # Response format
    
    collection = response.json()
    print(collection['type'] == 'FeatureCollection')
    print(sorted(collection.keys()))
    print(collection['query'])

    first = collection['features'][0]
    print(first['type'] == 'Feature')

    # Forward geocoding

    response = geocoder.forward('200 queen street')
    print(response.status_code)
    print(response.headers['Content-Type'])
    first = response.geojson()['features'][0]
    print(first['place_name'])

    # Forward geocoding with proximity

    response = geocoder.forward('200 queen street', lon=-66.05, lat=45.27)
    print(response.status_code)
    first = response.geojson()['features'][0]
    print(first['place_name'])
    print([int(coord) for coord in first['geometry']['coordinates']])

    # Forward geocoding with bounding box

    response = geocoder.forward('washington', bbox=[-78.338320, 38.520792, -77.935454, 38.864909], types=('place',))
    print(response.status_code)
    first = response.geojson()['features'][0]
    print(first['place_name'])
    print([round(coord,2) for coord in first['geometry']['coordinates']])

    # Forward geocoding with limited results

    response = geocoder.forward('washington', limit=3)
    print(response.status_code)
    print(len(response.geojson()['features']))

    # Reverse geocoding

    response = geocoder.reverse(lon=-73.989, lat=40.733)
    print(response.status_code)
    features = sorted(response.geojson()['features'], key = lambda x: x['place_name'])
    for f in features:
        print('{place_name}: {id}'.format(**f))

    # Reverse geocoding with limited results by location type

    response = geocoder.reverse(lon=-73.989, lat=40.733, limit=1, types=['country'])
    print(response.status_code)
    features = response.geojson()['features']
    print(len(features))

    print('{place_name}: {id}'.format(**features[0]))

    # Filtering by country code

    response = geocoder.forward('200 queen street', country=['us'])
    print(response.status_code)
    print(any(['Canada' in f['place_name'] for f in response.geojson()['features']]))

    # Filtering by type

    response = geocoder.reverse(lon=-73.989, lat=40.733, types=['poi'])
    print(response.status_code)
    features = response.geojson()['features']
    print(all([f['id'].startswith('poi') for f in features]))
Beispiel #9
0
def geocoding(ctx, query, forward, include_headers, lat, lon,
              place_type, output, dataset, country, bbox, features, limit):
    """This command returns places matching an address (forward mode) or
    places matching coordinates (reverse mode).

    In forward (the default) mode the query argument shall be an address
    such as '1600 pennsylvania ave nw'.

      $ mapbox geocoding '1600 pennsylvania ave nw'

    In reverse mode the query argument shall be a JSON encoded array
    of longitude and latitude (in that order) in decimal degrees.

      $ mapbox geocoding --reverse '[-77.4371, 37.5227]'

    An access token is required, see `mapbox --help`.
    """
    access_token = (ctx.obj and ctx.obj.get('access_token')) or None
    stdout = click.open_file(output, 'w')

    geocoder = Geocoder(name=dataset, access_token=access_token)

    if forward:
        if country:
            country = [x.lower() for x in country.split(",")]

        if bbox:
            try:
                bbox = tuple(map(float, bbox.split(',')))
            except ValueError:
                bbox = json.loads(bbox)

        for q in iter_query(query):
            try:
                resp = geocoder.forward(
                    q, types=place_type, lat=lat, lon=lon,
                    country=country, bbox=bbox, limit=limit)
            except mapbox.errors.ValidationError as exc:
                raise click.BadParameter(str(exc))

            if include_headers:
                echo_headers(resp.headers, file=stdout)
            if resp.status_code == 200:
                if features:
                    collection = json.loads(resp.text)
                    for feat in collection['features']:
                        click.echo(json.dumps(feat), file=stdout)
                else:
                    click.echo(resp.text, file=stdout)
            else:
                raise MapboxCLIException(resp.text.strip())
    else:
        for lon, lat in map(coords_from_query, iter_query(query)):
            try:
                resp = geocoder.reverse(
                    lon=lon, lat=lat, types=place_type, limit=limit)
            except mapbox.errors.ValidationError as exc:
                raise click.BadParameter(str(exc))

            if include_headers:
                echo_headers(resp.headers, file=stdout)
            if resp.status_code == 200:
                if features:
                    collection = json.loads(resp.text)
                    for feat in collection['features']:
                        click.echo(json.dumps(feat), file=stdout)
                else:
                    click.echo(resp.text, file=stdout)
            else:
                raise MapboxCLIException(resp.text.strip())