Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Looking for a good tutorial on how to access open-data via api.

Options
  • 26-03-2017 5:28pm
    #1
    Registered Users Posts: 226 ✭✭


    Hi Folks

    Can anyone point me in the direction of a really good tutorial on how to display open-data on a map. For example on data.gov.ie there is the Real-time Passenger Information (RTPI) for Dublin Bus, Bus Eireann, Luas and Irish rail data-set. I'd like to know how I can create a html page that calls that data and displays it. For example all the bus stops on a map.

    I'm very new to this. I have managed to take data from a Fusion Table and display it on a map. I'd be very grateful to anyone who gets back to me.

    Thanks


Comments

  • Registered Users Posts: 7,518 ✭✭✭matrim


    dakan wrote: »
    Hi Folks

    Can anyone point me in the direction of a really good tutorial on how to display open-data on a map. For example on data.gov.ie there is the Real-time Passenger Information (RTPI) for Dublin Bus, Bus Eireann, Luas and Irish rail data-set. I'd like to know how I can create a html page that calls that data and displays it. For example all the bus stops on a map.

    I'm very new to this. I have managed to take data from a Fusion Table and display it on a map. I'd be very grateful to anyone who gets back to me.

    Thanks

    You need to do a http request to their server with the data you require in the URL. Their response is in JSON (or XML), you decode that and then translate you you front end HTML.

    The exact method on how to do the above depends on the language you are using.

    There is information about the API at https://data.dublinked.ie/dataset/real-time-passenger-information-rtpi-for-dublin-bus-bus-eireann-luas-and-irish-rail


  • Registered Users Posts: 226 ✭✭dakan


    Hi matrim

    thanks for that. Problem is I don't know hwo to do any of that. I've searched online for tutorials but they all seem to do it via specific web developer apps. I'm looking for a tutorial on HTML or Java code on how to call that API and get any data back.

    Thanks


  • Registered Users Posts: 7,518 ✭✭✭matrim


    dakan wrote: »
    Hi matrim

    thanks for that. Problem is I don't know hwo to do any of that. I've searched online for tutorials but they all seem to do it via specific web developer apps. I'm looking for a tutorial on HTML or Java code on how to call that API and get any data back.

    Thanks

    What languages do you know? You mention Java, any others (JavaScript, python)?


  • Registered Users Posts: 6,251 ✭✭✭Buford T Justice


    You can't just write some html to do this. You need some sort of language to create the GET request to return the data and insert into the page.

    What languages do you know?


  • Registered Users Posts: 7,518 ✭✭✭matrim


    Here's an example of using the Dublin Bus RTPI API using Python. There is a good bit of boiler plate in it but the main parts for getting the API are below with comments starting with a #
    # build up the URL parameters
    params = {}
    params['stopid'] = self.stop
    
    if len(self.route) > 0:
        params['routeid'] = self.route
    
    params['maxresults'] = 2
    params['format'] = 'json'
    
    # Send the HTTP response
    response = requests.get(
        _RESOURCE,
        params,
        timeout=10)
    
    
    # Check if the request returned an error
    if response.status_code != 200:
        self.info = [{ATTR_DUE_AT: 'n/a',
                      ATTR_ROUTE: self.route,
                      ATTR_DUE_IN: 'n/a'}]
        return
    
    
    # Decode the JSON result
    result = response.json()
    
    # Check the JSON for an error
    if str(result['errorcode']) != '0':
        self.info = [{ATTR_DUE_AT: 'n/a',
                      ATTR_ROUTE: self.route,
                      ATTR_DUE_IN: 'n/a'}]
        return
    
    
    # Extract the results you want
    self.info = []
    for item in result['results']:
        due_at = item.get('departuredatetime')
        route = item.get('route')
        if due_at is not None and route is not None:
            bus_data = {ATTR_DUE_AT: due_at,
                        ATTR_ROUTE: route,
                        ATTR_DUE_IN:
                            due_in_minutes(due_at)}
            self.info.append(bus_data)
    

    Essentially what you are doing is a HTTP request formatted like:
    https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation?stopid=847&maxresults=2&format=json
    

    And getting a response formatted like:
    {
      "errorcode": "0",
      "errormessage": "",
      "numberofresults": 2,
      "stopid": "847",
      "timestamp": "27\/03\/2017 11:20:36",
      "results": [
        {
          "arrivaldatetime": "27\/03\/2017 11:22:54",
          "duetime": "2",
          "departuredatetime": "27\/03\/2017 11:22:54",
          "departureduetime": "2",
          "scheduledarrivaldatetime": "27\/03\/2017 11:24:00",
          "scheduleddeparturedatetime": "27\/03\/2017 11:24:00",
          "destination": "Ballywaltrim",
          "destinationlocalized": "Baile Bhaltraim",
          "origin": "Heuston Station",
          "originlocalized": "St\u00e1isiun Heuston",
          "direction": "Outbound",
          "operator": "bac",
          "additionalinformation": "",
          "lowfloorstatus": "no",
          "route": "145",
          "sourcetimestamp": "27\/03\/2017 11:16:11",
          "monitored": "true"
        },
        {
          "arrivaldatetime": "27\/03\/2017 11:27:52",
          "duetime": "7",
          "departuredatetime": "27\/03\/2017 11:27:52",
          "departureduetime": "7",
          "scheduledarrivaldatetime": "27\/03\/2017 11:28:00",
          "scheduleddeparturedatetime": "27\/03\/2017 11:28:00",
          "destination": "Dun Laoghaire",
          "destinationlocalized": "D\u00fan Laoghaire",
          "origin": "Infirmary Rd",
          "originlocalized": "Infirmary Rd",
          "direction": "Outbound",
          "operator": "bac",
          "additionalinformation": "",
          "lowfloorstatus": "no",
          "route": "46A",
          "sourcetimestamp": "27\/03\/2017 11:19:28",
          "monitored": "true"
        }
      ]
    }
    


  • Advertisement
  • Registered Users Posts: 226 ✭✭dakan


    Thanks Matrim

    I'll give that a try.


Advertisement