D. Example code – Retrieve a Conversion

 

 

 

Previous Table of Content Next

 

 

Appendix D. Example code – Retrieve a Conversion

This example is a function that given the code of a conversion, prints the conversion details.

 

This program will use the following endpoints: Conversion/.

 

 

Conversion/

 

 

The endpoint requires one parameter, the code (id) of the element to be retrieved. This is seen in Figure 15.

 

 

Figure 15 – /v1/Conversion/{id} endpoint parameters.

 

 

The returned information from the /v1/Conversion/ API endpoint is, according to Swagger:

 

 

{

  "ParameterValues": [

    {

      "ParameterCode": 0,

      "ParameterValue": 0,

      "ParamValueFileRef": "string",

      "Unit": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "SignReversible": true,

      "SortOrder": 0,

      "Code": 0,

      "Changes": [

        {

          "Code": 0,

          "Name": "string",

          "href": "string"

        }

      ],

      "Alias": [

        {

          "Code": 0,

          "Alias": "string",

          "NamingSystem": {

            "Code": 0,

            "Name": "string",

            "href": "string"

          },

          "Remark": "string"

        }

      ],

      "Links": [

        {

          "rel": "string",

          "href": "string"

        }

      ],

      "Name": "string",

      "Remark": "string",

      "DataSource": "string",

      "InformationSource": "string",

      "RevisionDate": "2021-06-29T12:59:19.887Z",

      "Deprecations": [

        {

          "Id": 0,

          "Date": "2021-06-29T12:59:19.887Z",

          "ChangeId": 0,

          "ReplacedBy": {

            "Code": 0,

            "Name": "string",

            "href": "string"

          },

          "Reason": "string"

        }

      ],

      "Supersessions": [

        {

          "Id": 0,

          "SupersededBy": {

            "Code": 0,

            "Name": "string",

            "href": "string"

          },

          "Year": 0,

          "Remarks": "string"

        }

      ]

    }

  ],

  "Method": {

    "Code": 0,

    "Name": "string",

    "href": "string"

  },

  "Usage": [

    {

      "Code": 0,

      "Name": "string",

      "ScopeDetails": "string",

      "Scope": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Extent": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Links": [

        {

          "rel": "string",

          "href": "string"

        }

      ],

      "Deprecation": [

        {

          "Id": 0,

          "Date": "2021-06-29T12:59:19.887Z",

          "ChangeId": 0,

          "ReplacedBy": {

            "Code": 0,

            "Name": "string",

            "href": "string"

          },

          "Reason": "string"

        }

      ],

      "Supersession": [

        {

          "Id": 0,

          "SupersededBy": {

            "Code": 0,

            "Name": "string",

            "href": "string"

          },

          "Year": 0,

          "Remarks": "string"

        }

      ]

    }

  ],

  "Code": 0,

  "Changes": [

    {

      "Code": 0,

      "Name": "string",

      "href": "string"

    }

  ],

  "Alias": [

    {

      "Code": 0,

      "Alias": "string",

      "NamingSystem": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Remark": "string"

    }

  ],

  "Links": [

    {

      "rel": "string",

      "href": "string"

    }

  ],

  "Name": "string",

  "Remark": "string",

  "DataSource": "string",

  "InformationSource": "string",

  "RevisionDate": "2021-06-29T12:59:19.888Z",

  "Deprecations": [

    {

      "Id": 0,

      "Date": "2021-06-29T12:59:19.888Z",

      "ChangeId": 0,

      "ReplacedBy": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Reason": "string"

    }

  ],

  "Supersessions": [

    {

      "Id": 0,

      "SupersededBy": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Year": 0,

      "Remarks": "string"

    }

  ]

}

 

 

From this information we are going to print the name of the conversion, the code of the conversion, the name of the method is uses and then iterate over the parameters and print their name and values. The values will be simply displayed with no unit and no specific formatting.

 

 

 Example code in Python (download)

 

 

# Python version 3.X

#

# This file contains functions that deal with conversions

#

#

import requests

from requests.auth import HTTPDigestAuth

import json

import sys

 

# Function call expects the ConversionCode to be a number, not a string

# ----------------------------------------------------------------------

def PrintConversion( ConversionCode ) :

    # Execute the get call

    myResponse = requests.get("https://apps.epsg.org/api/v1/Conversion/"+str(ConversionCode)+"/")

 

    # For successful API call, response code will be 200 (OK)

    if(myResponse.ok):

        # Loading the response data into a dictionary variable

        jData = json.loads(myResponse.content)

   

        print( "Code         :"+str(jData["Code"]))

        print( "Name         :"+jData["Name"])

        print( "Method       :"+jData["Method"]["Name"])

        print( "\nParameters:")

 

        for parameter in jData["ParameterValues"]:

            print("\tName :"+parameter["Name"])

            if parameter["ParameterValue"] is None:

                print("\t\tValue :"+parameter["ParameterValueFileRef"])

            else:

                print("\t\tValue :"+str(parameter["ParameterValue"]))

 

    else:

    # Assume the response was a failure of some kind and report it

        print( "Data for conversion "+str(ConversionCode)+" cound not be retrieved.")

          

def main():

      args = sys.argv[1:]

      if args:

             Search = args[0]

      else:

             print("This tool will print information on a Conversion given a conversion code")

             Search = input("Enter conversion code:")

 

      PrintConversion( int(Search) )

            

if __name__ == '__main__':

main()