LCOV - code coverage report
Current view: top level - shumate/vector - shumate-vector-layer.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 64 81 79.0 %
Date: 2024-05-11 21:41:31 Functions: 9 10 90.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 31 56 55.4 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2021 James Westman <james@jwestman.net>
       3                 :            :  *
       4                 :            :  * This library is free software; you can redistribute it and/or
       5                 :            :  * modify it under the terms of the GNU Lesser General Public
       6                 :            :  * License as published by the Free Software Foundation; either
       7                 :            :  * version 2.1 of the License, or (at your option) any later version.
       8                 :            :  *
       9                 :            :  * This library is distributed in the hope that it will be useful,
      10                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12                 :            :  * Lesser General Public License for more details.
      13                 :            :  *
      14                 :            :  * You should have received a copy of the GNU Lesser General Public
      15                 :            :  * License along with this library; if not, see <https://www.gnu.org/licenses/>.
      16                 :            :  */
      17                 :            : 
      18                 :            : #include <json-glib/json-glib.h>
      19                 :            : #include "shumate-vector-background-layer-private.h"
      20                 :            : #include "shumate-vector-expression-private.h"
      21                 :            : #include "shumate-vector-fill-layer-private.h"
      22                 :            : #include "shumate-vector-layer-private.h"
      23                 :            : #include "shumate-vector-line-layer-private.h"
      24                 :            : #include "shumate-vector-symbol-layer-private.h"
      25                 :            : 
      26                 :            : typedef struct
      27                 :            : {
      28                 :            :   GObject parent_instance;
      29                 :            : 
      30                 :            :   char *id;
      31                 :            : 
      32                 :            :   double minzoom;
      33                 :            :   double maxzoom;
      34                 :            :   char *source_layer;
      35                 :            :   ShumateVectorExpression *filter;
      36                 :            : 
      37                 :            : } ShumateVectorLayerPrivate;
      38                 :            : 
      39   [ +  +  +  - ]:        186 : G_DEFINE_TYPE_WITH_PRIVATE (ShumateVectorLayer, shumate_vector_layer, G_TYPE_OBJECT)
      40                 :            : 
      41                 :            : 
      42                 :            : ShumateVectorLayer *
      43                 :         24 : shumate_vector_layer_create_from_json (JsonObject *object, GError **error)
      44                 :            : {
      45                 :         24 :   ShumateVectorLayer *layer;
      46                 :         24 :   ShumateVectorLayerPrivate *priv;
      47                 :         24 :   JsonNode *filter;
      48                 :         24 :   const char *type = json_object_get_string_member_with_default (object, "type", NULL);
      49                 :            : 
      50         [ -  + ]:         24 :   if (type == NULL)
      51                 :            :     {
      52                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Expected element of \"layer\" to have a string member \"type\"");
      53                 :          0 :       return NULL;
      54                 :            :     }
      55                 :            : 
      56         [ +  + ]:         24 :   if (g_strcmp0 (type, "background") == 0)
      57                 :          6 :     layer = shumate_vector_background_layer_create_from_json (object, error);
      58         [ +  + ]:         18 :   else if (g_strcmp0 (type, "fill") == 0)
      59                 :          6 :     layer = shumate_vector_fill_layer_create_from_json (object, error);
      60         [ +  + ]:         12 :   else if (g_strcmp0 (type, "line") == 0)
      61                 :          6 :     layer = shumate_vector_line_layer_create_from_json (object, error);
      62         [ +  - ]:          6 :   else if (g_strcmp0 (type, "symbol") == 0)
      63                 :          6 :     layer = shumate_vector_symbol_layer_create_from_json (object, error);
      64                 :            :   else
      65                 :            :     {
      66                 :          0 :       g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unsupported layer type \"%s\"", type);
      67                 :          0 :       return NULL;
      68                 :            :     }
      69                 :            : 
      70         [ -  + ]:         24 :   if (layer == NULL)
      71                 :            :     /* A problem occurred in one of the constructors above, and error is already
      72                 :            :      * set */
      73                 :            :     return NULL;
      74                 :            : 
      75                 :         24 :   priv = shumate_vector_layer_get_instance_private (layer);
      76         [ -  + ]:         24 :   priv->id = g_strdup (json_object_get_string_member_with_default (object, "id", NULL));
      77                 :         24 :   priv->minzoom = json_object_get_double_member_with_default (object, "minzoom", 0.0);
      78                 :         24 :   priv->maxzoom = json_object_get_double_member_with_default (object, "maxzoom", 1000000000.0);
      79         [ -  + ]:         24 :   priv->source_layer = g_strdup (json_object_get_string_member_with_default (object, "source-layer", NULL));
      80                 :            : 
      81                 :         24 :   filter = json_object_get_member (object, "filter");
      82         [ -  + ]:         24 :   if (filter != NULL)
      83                 :            :     {
      84         [ #  # ]:          0 :       if (!(priv->filter = shumate_vector_expression_from_json (filter, error)))
      85                 :            :         return NULL;
      86                 :            :     }
      87                 :            : 
      88                 :            :   return layer;
      89                 :            : }
      90                 :            : 
      91                 :            : 
      92                 :            : static void
      93                 :         24 : shumate_vector_layer_finalize (GObject *object)
      94                 :            : {
      95                 :         24 :   ShumateVectorLayer *self = (ShumateVectorLayer *)object;
      96                 :         24 :   ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self);
      97                 :            : 
      98         [ +  - ]:         24 :   g_clear_pointer (&priv->id, g_free);
      99         [ +  + ]:         24 :   g_clear_pointer (&priv->source_layer, g_free);
     100         [ -  + ]:         24 :   g_clear_object (&priv->filter);
     101                 :            : 
     102                 :         24 :   G_OBJECT_CLASS (shumate_vector_layer_parent_class)->finalize (object);
     103                 :         24 : }
     104                 :            : 
     105                 :            : 
     106                 :            : static void
     107                 :          6 : shumate_vector_layer_class_init (ShumateVectorLayerClass *klass)
     108                 :            : {
     109                 :          6 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     110                 :          6 :   ShumateVectorLayerClass *layer_class = SHUMATE_VECTOR_LAYER_CLASS (klass);
     111                 :            : 
     112                 :          6 :   object_class->finalize = shumate_vector_layer_finalize;
     113                 :            : 
     114                 :          6 :   layer_class->render = NULL;
     115                 :            : }
     116                 :            : 
     117                 :            : static void
     118                 :         24 : shumate_vector_layer_init (ShumateVectorLayer *self)
     119                 :            : {
     120                 :         24 : }
     121                 :            : 
     122                 :            : 
     123                 :            : /**
     124                 :            :  * shumate_vector_layer_render:
     125                 :            :  * @self: a [class@VectorLayer]
     126                 :            :  *
     127                 :            :  * Renders the layer.
     128                 :            :  */
     129                 :            : void
     130                 :         12 : shumate_vector_layer_render (ShumateVectorLayer *self, ShumateVectorRenderScope *scope)
     131                 :            : {
     132                 :         12 :   ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self);
     133                 :            : 
     134         [ +  - ]:         12 :   g_return_if_fail (SHUMATE_IS_VECTOR_LAYER (self));
     135                 :            : 
     136   [ +  -  +  - ]:         12 :   if (scope->zoom_level < priv->minzoom || scope->zoom_level > priv->maxzoom)
     137                 :            :     return;
     138                 :            : 
     139         [ +  + ]:         12 :   if (priv->source_layer == NULL)
     140                 :            :     /* Style layers with no source layer are rendered once */
     141                 :          3 :     SHUMATE_VECTOR_LAYER_GET_CLASS (self)->render (self, scope);
     142         [ +  - ]:          9 :   else if (shumate_vector_reader_iter_read_layer_by_name (scope->reader, priv->source_layer))
     143                 :            :     {
     144                 :            :       /* Style layers with a source layer are rendered once for each feature
     145                 :            :        * in that layer, if it exists */
     146                 :            : 
     147                 :          9 :       VectorTile__Tile__Layer *layer = shumate_vector_reader_iter_get_layer_struct (scope->reader);
     148                 :            : 
     149         [ +  - ]:          9 :       if (layer->n_features == 0)
     150                 :            :         return;
     151                 :            : 
     152                 :          9 :       scope->source_layer_idx = shumate_vector_reader_iter_get_layer_index (scope->reader);
     153                 :          9 :       cairo_save (scope->cr);
     154                 :            : 
     155                 :          9 :       scope->scale = (double) layer->extent / scope->target_size / scope->overzoom_scale;
     156                 :          9 :       cairo_scale (scope->cr, 1.0 / scope->scale, 1.0 / scope->scale);
     157                 :            : 
     158                 :          9 :       cairo_translate (scope->cr, -scope->overzoom_x * layer->extent, -scope->overzoom_y * layer->extent);
     159                 :            : 
     160         [ -  + ]:          9 :       if (priv->filter != NULL)
     161                 :            :         {
     162                 :          0 :           int feature_idx = -1;
     163                 :          0 :           g_autoptr(ShumateVectorIndexBitset) bitset = NULL;
     164                 :            : 
     165                 :          0 :           shumate_vector_render_scope_index_layer (scope);
     166                 :          0 :           bitset = shumate_vector_expression_eval_bitset (priv->filter, scope, NULL);
     167                 :            : 
     168         [ #  # ]:          0 :           g_assert (bitset->len == layer->n_features);
     169                 :            : 
     170         [ #  # ]:          0 :           while ((feature_idx = shumate_vector_index_bitset_next (bitset, feature_idx)) != -1)
     171                 :            :             {
     172                 :          0 :               shumate_vector_reader_iter_read_feature (scope->reader, feature_idx);
     173                 :          0 :               SHUMATE_VECTOR_LAYER_GET_CLASS (self)->render (self, scope);
     174                 :            :             }
     175                 :            :         }
     176                 :            :       else
     177                 :            :         {
     178         [ +  + ]:         36 :           while (shumate_vector_reader_iter_next_feature (scope->reader))
     179                 :         27 :             SHUMATE_VECTOR_LAYER_GET_CLASS (self)->render (self, scope);
     180                 :            :         }
     181                 :            : 
     182                 :          9 :       cairo_restore (scope->cr);
     183                 :            :     }
     184                 :            : }
     185                 :            : 
     186                 :            : const char *
     187                 :         15 : shumate_vector_layer_get_id (ShumateVectorLayer *self)
     188                 :            : {
     189                 :         15 :   ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self);
     190         [ +  - ]:         15 :   g_return_val_if_fail (SHUMATE_IS_VECTOR_LAYER (self), NULL);
     191                 :         15 :   return priv->id;
     192                 :            : }
     193                 :            : 
     194                 :            : const char *
     195                 :          0 : shumate_vector_layer_get_source_layer (ShumateVectorLayer *self)
     196                 :            : {
     197                 :          0 :   ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self);
     198         [ #  # ]:          0 :   g_return_val_if_fail (SHUMATE_IS_VECTOR_LAYER (self), NULL);
     199                 :          0 :   return priv->source_layer;
     200                 :            : }
     201                 :            : 
     202                 :            : ShumateVectorExpression *
     203                 :         24 : shumate_vector_layer_get_filter (ShumateVectorLayer *self)
     204                 :            : {
     205                 :         24 :   ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self);
     206         [ +  - ]:         24 :   g_return_val_if_fail (SHUMATE_IS_VECTOR_LAYER (self), NULL);
     207                 :         24 :   return priv->filter;
     208                 :            : }

Generated by: LCOV version 1.14