Salta el contingut

API Database Layers

Create API

Through the DataBaseLayers application, we can create a public or private API for a database table connected to giscube-admin. The endpoint is auto-generated when we create a DataBaseLayer, and we can access it by opening the URL that appears in the administrator.

Screenshot

In the case of the above image, we will obtain the following link: (…)/layerserver/databaselayers/adreces/data/

Number of Results

Some APIs may return a large number of results, making requests very slow. To avoid this, we can “paginate” the results. The page_size field is used to limit (or paginate) the number of results returned by the API. To obtain all results, i.e., to avoid pagination, we set page_size=0.

Filtering Results (GeoJSON Layers)

In many cases, we will use the Endpoint of a DataBaseLayer to create GeoJSON Layers. GeoJSON Layers are obtained by filtering the results of the API. Below are some typical examples of this practice.

The giscube-admin application is a Django project, and all filters can be found on the Django documentation page. The filters are written using the Django “grammar”.

Queries are made by comparing one or more fields, starting with a ? and chaining (the filters for different fields) with an &. For example: (...)/layerserver/databaselayers/geo_covid/data/?page_size=0&sector_neteja=true

  • =: One of the most common filters is to filter a table by the value of a field, such as filtering a table by the ID of a foreign key. Example: field=5

  • icontains: Another commonly used filter is icontains. It checks if a field contains a text string, ignoring uppercase and lowercase letters. If we want to consider case sensitivity, we can use contains. Example: field__icontains=word or sentence

  • in: Para filtrar una capa por diferentes valores de un mismo campo se utiliza el sufijo in y se separan los diferentes valores con una coma. Exemple: field__in=value1,value2,value3,...

  • istartswith: It checks if the field starts with the specified text string. Similar to the previous one, we can use istartswith. field__startswith=Word

  • isnull: It checks if the field is null. field__isnull=False returns only the records where the field is not null.

  • gt/gte: It checks if the field is greater or equal. Example: field__gte=4. It can be chained with other filters, such as date filters. For example, to filter all records that are part of the second half of the year: field__month__gte=6

  • lt/lte: It checks if the field is less or equal. Example: field__lte=4

Geometric filtering is also possible. In this case, you do not have to specify which field contains the geometry; the API automatically detects it.

  • intersects: Checks if the geometry of the element intersects with the polygon provided as a parameter. The polygon is written as a list of coordinates separated by commas: intersects=x0,y0,x1,y1,x2,y2,x3,y3,...

  • in_bbox: Checks if there is an overlap between the geometry of the element and the bbox passed as a parameter. The bbox is written using WKT: in_bbox=SRID=4326; POLYGON ((x_min y_min, x_min y_max, x_max y_max, x_max y_min, x_min y_min))