E. Example code – Projected CRS and Extent

 

 

 

Previous Table of Content  

 

 

Appendix E. Example code – Projected CRS and Extent

This example asks the user to enter an EPSG code for a projected CRS. Data describing the CRS is then retrieved and all the usages linked to the projected CRS are also retrieved and details printed. Code 2393 “KKJ / Finland Uniform Coordinate System“ is an example of a projected CRS with more than 1 usage.

 

This program will use the following endpoints: ProjectedCoordRefSystem/ and Extent/.

 

 

ProjectedCoordRefSystem/

 

 

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

 

 

Figure 16 – /v1/ProjectedCoordRefSystem/{id} endpoint parameters.

 

 

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

 

 

{

  "BaseCoordRefSystem": {

    "Code": 0,

    "Name": "string",

    "href": "string"

  },

  "Projection": {

    "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-05-26T11:24:55.164Z",

          "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"

        }

      ]

    }

  ],

  "CoordSys": {

    "Code": 0,

    "Name": "string",

    "href": "string"

  },

  "Kind": "string",

  "Deformations": [

    {

      "Code": 0,

      "Name": "string",

      "href": "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-05-26T11:24:55.165Z",

  "Deprecations": [

    {

      "Id": 0,

      "Date": "2021-05-26T11:24:55.165Z",

      "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"

    }

  ]

}

 

 

Extent/

 

 

Similar to the previous endpoint, this one also requires only one parameter, the code (id).

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

 

 

{

  "Description": "string",

  "BoundingBoxSouthBoundLatitude": 0,

  "BoundingBoxWestBoundLongitude": 0,

  "BoundingBoxEastBoundLongitude": 0,

  "BoundingBoxNorthBoundLatitude": 0,

  "IsoA2Code": "string",

  "IsoA3Code": "string",

  "IsoNCode": 0,

  "VerticalExtentMin": 0,

  "VerticalExtentMax": 0,

  "VerticalExtentCrs": {

    "Code": 0,

    "Name": "string",

    "href": "string"

  },

  "TemporalExtentBegin": "string",

  "TemporalExtentEnd": "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-05-26T11:24:54.907Z",

  "Deprecations": [

    {

      "Id": 0,

      "Date": "2021-05-26T11:24:54.907Z",

      "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"

    }

  ]

}

 

 

Using the endpoint /v1/ProjectedCoordRefSystem, we retrieve the parameters for a given Projected Coordinate Reference System from the database. If this is not successful, we assume that a code 500 return indicates that the requested code (id) is not a projected CRS and print a message accordingly. This is a simplification but for this demo, it works.

 

With the data for the requested code (id) returned, some of the basic information, Name, Base CRS name, Projection name, CS name and the type of element is printed out.

 

Usages, structure seen below, are then iterated over and for each available usage the Extent code is used to retrieve information for the database on the extent(s) using the /v1/Extent/{id}/ endpoint.

 

 

"Usage": [

    {

      "Code": 0,

      "Name": "string",

      "ScopeDetails": "string",

      "Scope": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Extent": {

        "Code": 0,

        "Name": "string",

        "href": "string"

      },

      "Links": [],

      "Deprecation": [],

      "Supersession": []

    }

  ],

 

 

For each found extent, its Code, Name, bounding box and description is printed.

 

 

Example code in Python (download)

 

 

# Python version 3.X

import requests

from requests.auth import HTTPDigestAuth

import json

 

# Replace with the correct URL

url = "https://apps.epsg.org/api/v1/ProjectedCoordRefSystem/"

print("Given an EPSG code for a projected CRS, this tool returns the name and prints the extent data for all usages")

Search = input("What code are you looking for ? ")

# looks odd but is just throwing an error if the entered value is not possible to convert to an int.

value = int(Search)

Search = str(value)

RequestURL = url+Search+"/"

myResponse = requests.get(RequestURL)

 

# 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("CRS name   : "+jData["Name"])

    print("Base CRS   : "+jData["BaseCoordRefSystem"]["Name"]) 

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

    print("Coord sys  : "+jData["CoordSys"]["Name"])     

    print("Type       : "+jData["Kind"])     

    # Now iterate the usages

    for usage in jData["Usage"]:

        print("Extent Code : " + str(usage["Extent"]["Code"]))

        # Go and get the Extent data

        myResponse = requests.get("https://apps.epsg.org/api/v1/Extent/" + str(usage["Extent"]["Code"]))

        if myResponse.ok:

            extentData = json.loads(myResponse.content)

            print("Extent name : " + extentData["Name"])

            print("Extent desc : " + extentData["Description"])

            print("BBX North   : " + str(extentData["BoundingBoxNorthBoundLatitude"]))

            print("BBX West    : " + str(extentData["BoundingBoxWestBoundLongitude"]))

            print("BBX East   : " + str(extentData["BoundingBoxEastBoundLongitude"]))

            print("BBX South   : " + str(extentData["BoundingBoxSouthBoundLatitude"]))

else:

    # If response code is not ok (200), print the resulting http error code with description

    if (myResponse.status_code==500):

        print(Search+" code is not a projected CRS")

    else:

        myResponse.raise_for_status()