shumate/vector/shumate-vector-layer.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 |
4/6shumate_vector_layer_class_intern_init:
✓ Branch 3 → 4 taken 4 times.
✗ Branch 3 → 5 not taken.
shumate_vector_layer_get_type:
✓ Branch 2 → 3 taken 4 times.
✓ Branch 2 → 7 taken 54 times.
✓ Branch 4 → 5 taken 4 times.
✗ Branch 4 → 7 not taken.
|
156 | 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 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 24 times.
|
24 | if (type == NULL) |
| 51 | { | ||
| 52 | ✗ | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Expected element of \"layer\" to have a string member \"type\""); | |
| 53 | ✗ | return NULL; | |
| 54 | } | ||
| 55 | |||
| 56 |
2/2✓ Branch 8 → 9 taken 6 times.
✓ Branch 8 → 10 taken 18 times.
|
24 | if (g_strcmp0 (type, "background") == 0) |
| 57 | 6 | layer = shumate_vector_background_layer_create_from_json (object, error); | |
| 58 |
2/2✓ Branch 11 → 12 taken 6 times.
✓ Branch 11 → 13 taken 12 times.
|
18 | else if (g_strcmp0 (type, "fill") == 0) |
| 59 | 6 | layer = shumate_vector_fill_layer_create_from_json (object, error); | |
| 60 |
2/2✓ Branch 14 → 15 taken 6 times.
✓ Branch 14 → 16 taken 6 times.
|
12 | else if (g_strcmp0 (type, "line") == 0) |
| 61 | 6 | layer = shumate_vector_line_layer_create_from_json (object, error); | |
| 62 |
1/2✓ Branch 17 → 18 taken 6 times.
✗ Branch 17 → 19 not taken.
|
6 | else if (g_strcmp0 (type, "symbol") == 0) |
| 63 | 6 | layer = shumate_vector_symbol_layer_create_from_json (object, error); | |
| 64 | else | ||
| 65 | { | ||
| 66 | ✗ | g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unsupported layer type \"%s\"", type); | |
| 67 | ✗ | return NULL; | |
| 68 | } | ||
| 69 | |||
| 70 |
1/2✓ Branch 22 → 23 taken 24 times.
✗ Branch 22 → 48 not taken.
|
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 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 24 times.
|
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 |
1/2✗ Branch 35 → 36 not taken.
✓ Branch 35 → 37 taken 24 times.
|
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 |
1/2✗ Branch 44 → 45 not taken.
✓ Branch 44 → 48 taken 24 times.
|
24 | if (filter != NULL) |
| 83 | { | ||
| 84 | ✗ | 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 |
1/2✓ Branch 2 → 3 taken 24 times.
✗ Branch 2 → 4 not taken.
|
24 | g_clear_pointer (&priv->id, g_free); |
| 99 |
2/2✓ Branch 4 → 5 taken 18 times.
✓ Branch 4 → 6 taken 6 times.
|
24 | g_clear_pointer (&priv->source_layer, g_free); |
| 100 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 24 times.
|
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 | 4 | shumate_vector_layer_class_init (ShumateVectorLayerClass *klass) | |
| 108 | { | ||
| 109 | 4 | GObjectClass *object_class = G_OBJECT_CLASS (klass); | |
| 110 | 4 | ShumateVectorLayerClass *layer_class = SHUMATE_VECTOR_LAYER_CLASS (klass); | |
| 111 | |||
| 112 | 4 | object_class->finalize = shumate_vector_layer_finalize; | |
| 113 | |||
| 114 | 4 | 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 | 8 | shumate_vector_layer_render (ShumateVectorLayer *self, ShumateVectorRenderScope *scope) | |
| 131 | { | ||
| 132 | 8 | ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self); | |
| 133 | |||
| 134 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 8 times.
|
8 | g_return_if_fail (SHUMATE_IS_VECTOR_LAYER (self)); |
| 135 | |||
| 136 |
2/4✓ Branch 5 → 6 taken 8 times.
✗ Branch 5 → 31 not taken.
✓ Branch 6 → 7 taken 8 times.
✗ Branch 6 → 31 not taken.
|
8 | if (scope->zoom_level < priv->minzoom || scope->zoom_level > priv->maxzoom) |
| 137 | return; | ||
| 138 | |||
| 139 |
2/2✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 9 taken 6 times.
|
8 | if (priv->source_layer == NULL) |
| 140 | /* Style layers with no source layer are rendered once */ | ||
| 141 | 2 | SHUMATE_VECTOR_LAYER_GET_CLASS (self)->render (self, scope); | |
| 142 |
1/2✓ Branch 10 → 11 taken 6 times.
✗ Branch 10 → 31 not taken.
|
6 | 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 | 6 | VectorTile__Tile__Layer *layer = shumate_vector_reader_iter_get_layer_struct (scope->reader); | |
| 148 | |||
| 149 |
1/2✓ Branch 12 → 13 taken 6 times.
✗ Branch 12 → 31 not taken.
|
6 | if (layer->n_features == 0) |
| 150 | return; | ||
| 151 | |||
| 152 | 6 | scope->source_layer_idx = shumate_vector_reader_iter_get_layer_index (scope->reader); | |
| 153 | 6 | cairo_save (scope->cr); | |
| 154 | |||
| 155 | 6 | scope->scale = (double) layer->extent / scope->target_size / scope->overzoom_scale; | |
| 156 | 6 | cairo_scale (scope->cr, 1.0 / scope->scale, 1.0 / scope->scale); | |
| 157 | |||
| 158 | 6 | cairo_translate (scope->cr, -scope->overzoom_x * layer->extent, -scope->overzoom_y * layer->extent); | |
| 159 | |||
| 160 |
1/2✗ Branch 17 → 18 not taken.
✓ Branch 17 → 28 taken 6 times.
|
6 | if (priv->filter != NULL) |
| 161 | { | ||
| 162 | ✗ | int feature_idx = -1; | |
| 163 | 6 | g_autoptr(ShumateVectorIndexBitset) bitset = NULL; | |
| 164 | |||
| 165 | ✗ | shumate_vector_render_scope_index_layer (scope); | |
| 166 | ✗ | bitset = shumate_vector_expression_eval_bitset (priv->filter, scope, NULL); | |
| 167 | |||
| 168 | ✗ | g_assert (bitset->len == layer->n_features); | |
| 169 | |||
| 170 | ✗ | while ((feature_idx = shumate_vector_index_bitset_next (bitset, feature_idx)) != -1) | |
| 171 | { | ||
| 172 | ✗ | shumate_vector_reader_iter_read_feature (scope->reader, feature_idx); | |
| 173 | ✗ | SHUMATE_VECTOR_LAYER_GET_CLASS (self)->render (self, scope); | |
| 174 | } | ||
| 175 | } | ||
| 176 | else | ||
| 177 | { | ||
| 178 |
2/2✓ Branch 29 → 27 taken 18 times.
✓ Branch 29 → 30 taken 6 times.
|
24 | while (shumate_vector_reader_iter_next_feature (scope->reader)) |
| 179 | 18 | SHUMATE_VECTOR_LAYER_GET_CLASS (self)->render (self, scope); | |
| 180 | } | ||
| 181 | |||
| 182 | 6 | cairo_restore (scope->cr); | |
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | const char * | ||
| 187 | 10 | shumate_vector_layer_get_id (ShumateVectorLayer *self) | |
| 188 | { | ||
| 189 | 10 | ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self); | |
| 190 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 10 times.
|
10 | g_return_val_if_fail (SHUMATE_IS_VECTOR_LAYER (self), NULL); |
| 191 | 10 | return priv->id; | |
| 192 | } | ||
| 193 | |||
| 194 | const char * | ||
| 195 | ✗ | shumate_vector_layer_get_source_layer (ShumateVectorLayer *self) | |
| 196 | { | ||
| 197 | ✗ | ShumateVectorLayerPrivate *priv = shumate_vector_layer_get_instance_private (self); | |
| 198 | ✗ | g_return_val_if_fail (SHUMATE_IS_VECTOR_LAYER (self), NULL); | |
| 199 | ✗ | 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 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 24 times.
|
24 | g_return_val_if_fail (SHUMATE_IS_VECTOR_LAYER (self), NULL); |
| 207 | 24 | return priv->filter; | |
| 208 | } | ||
| 209 |