@icgcat/territori 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,166 @@
1
+ # @icgcat/territori
2
+
3
+ **@icgcat/territori** is a Svelte component library designed to interact with territorial services provided by the ICGC. It allows for the visualization of municipalities, counties, and cadastral data on an interactive map using **MapLibre**.
4
+
5
+ ## Features
6
+ - **Municipality (`Municipi`)**: Displays information about a selected municipality on the map.
7
+ - **County (`Comarca`)**: Displays information about a selected county on the map.
8
+ - **Cadastre (`Cadastre`)**: Displays cadastral information for a selected area on the map.
9
+ - Compatible with **MapLibre GL** for rendering data as map layers.
10
+
11
+ ## Installation
12
+ Install the package via npm:
13
+ ```bash
14
+ npm install @icgcat/territori
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ ### Municipality (Municipi)
20
+
21
+ The `Municipi` component retrieves and displays information about a municipality when clicking on the map.
22
+
23
+ **Basic example:**
24
+ ```js
25
+ <script>
26
+ import { Municipi } from "@icgcat/territori";
27
+ import maplibregl from "maplibre-gl";
28
+
29
+ let map;
30
+
31
+ onMount(() => {
32
+ map = new maplibregl.Map({
33
+ container: "map",
34
+ style: "https://geoserveis.icgc.cat/contextmaps/icgc_mapa_base_fosc.json",
35
+ center: [2.1734, 41.3851],
36
+ zoom: 10,
37
+ });
38
+ });
39
+ </script>
40
+
41
+ <Municipi map={map} color="#ffcc00" />
42
+ <div id="map" style="height: 500px;"></div>
43
+
44
+ ```
45
+ **Props:**
46
+
47
+ - `map`: (Required) A MapLibre GL instance.
48
+ - `color`: (Optional) Fill color for the municipality layer. Defaults to #f0f0f0 if not provided.
49
+
50
+ ##
51
+
52
+ ### County (Comarca)
53
+
54
+ The `Comarca` component works similarly to `Municipi`, but retrieves and displays county data.
55
+
56
+ **Example:**
57
+
58
+ ```js
59
+ <script>
60
+ import { Comarca } from "@icgcat/territori";
61
+
62
+ let map;
63
+
64
+ onMount(() => {
65
+ map = new maplibregl.Map({
66
+ container: "map",
67
+ style: "https://geoserveis.icgc.cat/contextmaps/icgc_mapa_base_fosc.json",
68
+ center: [1.5200, 41.3000],
69
+ zoom: 8,
70
+ });
71
+ });
72
+ </script>
73
+
74
+ <Comarca map={map} color="#8a2be2" />
75
+ <div id="map" style="height: 500px;"></div>
76
+
77
+ ```
78
+ **Props:**
79
+
80
+ - `map`: (Required) A MapLibre GL instance.
81
+ - `color`: (Optional) Fill color for the municipality layer. Defaults to #f0f0f0 if not provided.
82
+
83
+ ##
84
+
85
+ ### Cadastre (Cadastre)
86
+
87
+ The `Cadastre` component retrieves cadastral data and displays it on the map.
88
+
89
+ **Example:**
90
+
91
+ ```js
92
+ <script>
93
+ import { Cadastre } from "@icgcat/territori";
94
+
95
+ let map;
96
+
97
+ onMount(() => {
98
+ map = new maplibregl.Map({
99
+ container: "map",
100
+ style: "https://geoserveis.icgc.cat/contextmaps/icgc_mapa_base_fosc.json",
101
+ center: [2.1500, 41.3800],
102
+ zoom: 14,
103
+ });
104
+ });
105
+ </script>
106
+
107
+ <Cadastre map={map} color="#00cc66" />
108
+ <div id="map" style="height: 500px;"></div>
109
+
110
+ ```
111
+
112
+ **Props:**
113
+
114
+ - `map`: (Required) A MapLibre GL instance.
115
+ - `color`: (Optional) Fill color for the municipality layer. Defaults to #f0f0f0 if not provided.
116
+
117
+ <br>
118
+
119
+ ## Behavior
120
+
121
+ For all components:
122
+ 1. When the user clicks on the map, the component sends a request to the ICGC territorial API to fetch data for the clicked coordinates.
123
+ 2. The data is displayed as a layer on the map with the specified or default fill color.
124
+ 3. If no data is available, the existing layer is removed (if present).
125
+
126
+ ## Styling
127
+
128
+ Each component includes default styles for displaying data. You can customize these styles as needed.
129
+
130
+ ```css
131
+ #apinfo {
132
+ position: fixed;
133
+ top: 10px;
134
+ border-radius: 8px;
135
+ padding: 10px;
136
+ background-color: rgb(221, 220, 220);
137
+ color: black;
138
+ border: 1px solid rgb(87, 87, 87);
139
+ }
140
+ ```
141
+
142
+ ## API Endpoints
143
+
144
+ The components use the following ICGC API endpoints:
145
+
146
+ - Municipi: `https://api.icgc.cat/territori/municipis/geo/{lon}/{lat}`
147
+ - Comarca: `https://api.icgc.cat/territori/comarques/geo/{lon}/{lat}`
148
+ - Cadastre: `https://api.icgc.cat/territori/cadastre/geo/{lon}/{lat}`
149
+
150
+ ## Contributing
151
+
152
+ To contribute to this project:
153
+
154
+ 1. Fork the repository.
155
+ 2. Create a new branch (`git checkout -b feature-new-feature`).
156
+ 3. Commit your changes (`git commit -m 'Add new feature'`).
157
+ 4. Push to the branch (`git push origin feature-new-feature`).
158
+ 5. Open a pull request.
159
+
160
+ ## License
161
+
162
+ This project is licensed under the MIT License.
163
+
164
+ ##
165
+
166
+ For further inquiries or support, please contact the [ICGC development team](https://icgc.cat).
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@icgcat/territori",
3
+ "version": "0.0.1",
4
+ "main": "src/index.js",
5
+ "type": "module",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "build": "vite build"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "keywords": [],
14
+ "author": "Geostarters",
15
+ "license": "ISC",
16
+ "description": "A Svelte API Territorial component",
17
+ "devDependencies": {
18
+ "@sveltejs/vite-plugin-svelte": "^5.0.3",
19
+ "svelte": "^5.0.0",
20
+ "svelte-preprocess": "^6.0.3",
21
+ "vite": "^6.0.3"
22
+ }
23
+ }
@@ -0,0 +1,91 @@
1
+ <script>
2
+ export const componentProps = {
3
+ map: { default: null },
4
+ color: { default: undefined }, // Es permet no especificar color
5
+ };
6
+
7
+ const { map, color } = $props();
8
+ let cadastre = $state();
9
+
10
+ // Valor per defecte del color
11
+ const fillColor = color || "#f0f0f0"; // Utilitza el color o un valor predeterminat
12
+
13
+ map.on("click", function (e) {
14
+ let lon = e.lngLat.lng;
15
+ let lat = e.lngLat.lat;
16
+ apiConnect(lat, lon, "cadastre");
17
+ });
18
+
19
+ async function apiConnect(lat, lon, service) {
20
+ let response;
21
+ try {
22
+ response = await fetch(
23
+ `https://api.icgc.cat/territori/${service}/geo/${lon}/${lat}`,
24
+ );
25
+ } catch (error) {
26
+ console.error("Error", error);
27
+ }
28
+ const dades = await response.json();
29
+ if (dades.responses) {
30
+ cadastre = dades.responses.features[0].properties.localId;
31
+
32
+ if (!map.getLayer("punts")) {
33
+ map.addSource("punts", {
34
+ type: "geojson",
35
+ data: {
36
+ type: "FeatureCollection",
37
+ features: dades.responses.features,
38
+ },
39
+ });
40
+ map.addLayer({
41
+ id: "punts",
42
+ type: "fill",
43
+ source: "punts",
44
+ paint: {
45
+ "fill-color": fillColor,
46
+ "fill-opacity": 0.6,
47
+ },
48
+ });
49
+ } else {
50
+ map.removeLayer("punts").removeSource("punts");
51
+ map.addSource("punts", {
52
+ type: "geojson",
53
+ data: {
54
+ type: "FeatureCollection",
55
+ features: dades.responses.features,
56
+ },
57
+ });
58
+ map.addLayer({
59
+ id: "punts",
60
+ type: "fill",
61
+ source: "punts",
62
+ paint: {
63
+ "fill-color": fillColor,
64
+ "fill-opacity": 0.75,
65
+ },
66
+ });
67
+ }
68
+ } else {
69
+ cadastre = dades.error;
70
+ if (map.getLayer("punts")) {
71
+ map.removeLayer("punts").removeSource("punts");
72
+ }
73
+ }
74
+ }
75
+ </script>
76
+
77
+ {#if cadastre}
78
+ <div id="apinfo">{cadastre}</div>
79
+ {/if}
80
+
81
+ <style>
82
+ #apinfo {
83
+ position: fixed;
84
+ top: 10px;
85
+ border-radius: 8px;
86
+ padding: 10px;
87
+ background-color: rgb(221, 220, 220);
88
+ color: black;
89
+ border: 1px solid rgb(87, 87, 87);
90
+ }
91
+ </style>
@@ -0,0 +1,91 @@
1
+ <script>
2
+ export const componentProps = {
3
+ map: { default: null },
4
+ color: { default: undefined }, // Es permet no especificar color
5
+ };
6
+
7
+ const { map, color } = $props();
8
+ let comarca = $state();
9
+
10
+ // Valor per defecte del color
11
+ const fillColor = color || "#f0f0f0"; // Utilitza el color o un valor predeterminat
12
+
13
+ map.on("click", function (e) {
14
+ let lon = e.lngLat.lng;
15
+ let lat = e.lngLat.lat;
16
+ apiConnect(lat, lon, "comarques");
17
+ });
18
+
19
+ async function apiConnect(lat, lon, service) {
20
+ let response;
21
+ try {
22
+ response = await fetch(
23
+ `https://api.icgc.cat/territori/${service}/geo/${lon}/${lat}`,
24
+ );
25
+ } catch (error) {
26
+ console.error("Error", error);
27
+ }
28
+ const dades = await response.json();
29
+ if (dades.responses) {
30
+ comarca = dades.responses.features[0].properties.NOMCOMAR;
31
+
32
+ if (!map.getLayer("punts")) {
33
+ map.addSource("punts", {
34
+ type: "geojson",
35
+ data: {
36
+ type: "FeatureCollection",
37
+ features: dades.responses.features,
38
+ },
39
+ });
40
+ map.addLayer({
41
+ id: "punts",
42
+ type: "fill",
43
+ source: "punts",
44
+ paint: {
45
+ "fill-color": fillColor,
46
+ "fill-opacity": 0.6,
47
+ },
48
+ });
49
+ } else {
50
+ map.removeLayer("punts").removeSource("punts");
51
+ map.addSource("punts", {
52
+ type: "geojson",
53
+ data: {
54
+ type: "FeatureCollection",
55
+ features: dades.responses.features,
56
+ },
57
+ });
58
+ map.addLayer({
59
+ id: "punts",
60
+ type: "fill",
61
+ source: "punts",
62
+ paint: {
63
+ "fill-color": fillColor,
64
+ "fill-opacity": 0.75,
65
+ },
66
+ });
67
+ }
68
+ } else {
69
+ comarca = dades.error;
70
+ if (map.getLayer("punts")) {
71
+ map.removeLayer("punts").removeSource("punts");
72
+ }
73
+ }
74
+ }
75
+ </script>
76
+
77
+ {#if comarca}
78
+ <div id="apinfo">{comarca}</div>
79
+ {/if}
80
+
81
+ <style>
82
+ #apinfo {
83
+ position: fixed;
84
+ top: 10px;
85
+ border-radius: 8px;
86
+ padding: 10px;
87
+ background-color: rgb(221, 220, 220);
88
+ color: black;
89
+ border: 1px solid rgb(87, 87, 87);
90
+ }
91
+ </style>
@@ -0,0 +1,91 @@
1
+ <script>
2
+ export const componentProps = {
3
+ map: { default: null },
4
+ color: { default: undefined }, // Es permet no especificar color
5
+ };
6
+
7
+ const { map, color } = $props();
8
+ let municipi = $state();
9
+
10
+ // Valor per defecte del color
11
+ const fillColor = color || "#f0f0f0"; // Utilitza el color o un valor predeterminat
12
+
13
+ map.on("click", function (e) {
14
+ let lon = e.lngLat.lng;
15
+ let lat = e.lngLat.lat;
16
+ apiConnect(lat, lon, "municipis");
17
+ });
18
+
19
+ async function apiConnect(lat, lon, service) {
20
+ let response;
21
+ try {
22
+ response = await fetch(
23
+ `https://api.icgc.cat/territori/${service}/geo/${lon}/${lat}`,
24
+ );
25
+ } catch (error) {
26
+ console.error("Error", error);
27
+ }
28
+ const dades = await response.json();
29
+ if (dades.responses) {
30
+ municipi = dades.responses.features[0].properties.NOMMUNI;
31
+
32
+ if (!map.getLayer("punts")) {
33
+ map.addSource("punts", {
34
+ type: "geojson",
35
+ data: {
36
+ type: "FeatureCollection",
37
+ features: dades.responses.features,
38
+ },
39
+ });
40
+ map.addLayer({
41
+ id: "punts",
42
+ type: "fill",
43
+ source: "punts",
44
+ paint: {
45
+ "fill-color": fillColor,
46
+ "fill-opacity": 0.6,
47
+ },
48
+ });
49
+ } else {
50
+ map.removeLayer("punts").removeSource("punts");
51
+ map.addSource("punts", {
52
+ type: "geojson",
53
+ data: {
54
+ type: "FeatureCollection",
55
+ features: dades.responses.features,
56
+ },
57
+ });
58
+ map.addLayer({
59
+ id: "punts",
60
+ type: "fill",
61
+ source: "punts",
62
+ paint: {
63
+ "fill-color": fillColor,
64
+ "fill-opacity": 0.75,
65
+ },
66
+ });
67
+ }
68
+ } else {
69
+ municipi = dades.error;
70
+ if (map.getLayer("punts")) {
71
+ map.removeLayer("punts").removeSource("punts");
72
+ }
73
+ }
74
+ }
75
+ </script>
76
+
77
+ {#if municipi}
78
+ <div id="apinfo">{municipi}</div>
79
+ {/if}
80
+
81
+ <style>
82
+ #apinfo {
83
+ position: fixed;
84
+ top: 10px;
85
+ border-radius: 8px;
86
+ padding: 10px;
87
+ background-color: rgb(221, 220, 220);
88
+ color: black;
89
+ border: 1px solid rgb(87, 87, 87);
90
+ }
91
+ </style>
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ import Municipi from "./Municipi.svelte";
2
+ import Comarca from "./Comarca.svelte";
3
+ import Cadastre from "./Cadastre.svelte";
4
+ export { Municipi, Comarca, Cadastre };
package/vite.config.js ADDED
@@ -0,0 +1,23 @@
1
+ import { defineConfig } from "vite";
2
+ import { svelte } from "@sveltejs/vite-plugin-svelte";
3
+ import { sveltePreprocess } from "svelte-preprocess";
4
+
5
+ export default defineConfig({
6
+ plugins: [svelte({ preprocess: sveltePreprocess() })],
7
+ build: {
8
+ lib: {
9
+ entry: "src/index.js",
10
+ name: "Territori",
11
+ formats: ["es", "cjs", "umd"],
12
+ fileName: (format) => `territori.${format}.js`,
13
+ },
14
+ rollupOptions: {
15
+ external: ["svelte"],
16
+ output: {
17
+ globals: {
18
+ svelte: "Svelte",
19
+ },
20
+ },
21
+ },
22
+ },
23
+ });