Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Reverse Geocoding Example Using PositionStack and Python Flask

Reverse Geocoding Example Using PositionStack And Python Flask

This Restful API tutorial help to access Positionstack API Using python3. I am using flask rest framework which is based on python 3.

The Positionstack API help to get data from location information like address, name of place, latitude/langitude, IP address into the location on the earth surface for that place. The Positionstack provides two way to handle geocoding: forward geocoding and reverse geocoding.

  • Forward Geocoding: Geocoding by free-text place name or address.
  • Reverse Geocoding: Geocoding by coordinates or IP address.

There are following features available in Positionstack API –

  • Forward Geocoding
  • Reverse Geocoding
  • Batch Requests
  • Embeddable Maps
  • JSON, XML & GeoJSON
  • Multiple Languages

How To Access Location Information Using API

Let’s create an account into the Positionstack. We will get an APIkey. This API key will use to required to authenticate with the API.You can reset it at any time in your Account Dashboard.

The sample get call is –
https://api.positionstack.com/v1/forward? access_key = YOUR_ACCESS_KEY & & query = address

Where –

  • access_key : This is the mandatory field.
  • query : This is the mandatory field.

There are some optional field –

// optional parameters: 
 & limit = 10
 & output = json
// more parameters available

Create Python 3 App and Install Dependencies

I will create geocoding-python-example folder and install dependencies using pip. We will use requests package to handle HTTP request. You can install request and flask package using following command –

pip install requests
pip install flask

HTTP Get Request To Fetch Geo Data

I am using reverse API to get data from Positionstack API.

from flask import Flask
from flask_restful import Resource, Api
from flask_cors import CORS
import requests

app = Flask(__name__)
CORS(app) ## To allow direct AJAX calls

@app.route('/reverse_geo', methods=['GET'])
def home():
   try:        
    headers = {}
    res = requests.get('https://api.positionstack.com/v1/reverse?access_key=YOUR_ACCESS_KEY&query= 40.7638435,-73.9729691', headers=headers)

    return r.json()

   except Exception as e:
    print('unable to get Positionstack data.')
    print(e.strerror)

if __name__ == '__main__':
   app.run(debug = True)

The post Reverse Geocoding Example Using PositionStack and Python Flask appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

Reverse Geocoding Example Using PositionStack and Python Flask

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×