@osimatic/helpers-js 1.4.12 → 1.4.14

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/CHANGELOG CHANGED
@@ -78,4 +78,5 @@ HTTPRequest -> HTTPClient
78
78
  PostalAddress.setAutocomplete(input, onChange(formatted_address)) -> PostalAddress.setAutocomplete(input, onChange(place));
79
79
 
80
80
  1.4.0
81
- new OpenStreetMap(id) -> new OpenStreetMap(element)
81
+ new OpenStreetMap(id) -> new OpenStreetMap(element)
82
+ OpenStreetMap.centerOnFrance -> OpenStreetMap.centerMapOnCountry('FR')
package/location.js CHANGED
@@ -452,6 +452,21 @@ class Polygon {
452
452
  };
453
453
  }
454
454
 
455
+ static getStartCoordinates(geoJsonPolygon) {
456
+ if (typeof geoJsonPolygon == 'string') {
457
+ geoJsonPolygon = JSON.parse(geoJsonPolygon);
458
+ }
459
+
460
+ const rings = geoJsonPolygon.coordinates || [];
461
+ const ring0 = rings[0] || [];
462
+ if (ring0.length === 0) {
463
+ return null;
464
+ }
465
+
466
+ const [firstLon, firstLat] = ring0[0];
467
+ return [firstLat, firstLon];
468
+ }
469
+
455
470
  static convertToGeoJson(latlngs) {
456
471
  let rings = [];
457
472
  if (Array.isArray(latlngs) && Array.isArray(latlngs[0])) {
@@ -475,6 +490,40 @@ class Polygon {
475
490
  return { type: 'Polygon', coordinates: rings };
476
491
  }
477
492
 
493
+ static toLatLngRings(geoJsonPolygon) {
494
+ if (typeof geoJsonPolygon == 'string') {
495
+ geoJsonPolygon = JSON.parse(geoJsonPolygon);
496
+ }
497
+
498
+ const polygonCoords = geoJsonPolygon.coordinates;
499
+ if (!Array.isArray(polygonCoords) || polygonCoords.length === 0) {
500
+ return false;
501
+ }
502
+
503
+ // polygonCoords: [ [ [lon,lat], ... ] , ... ]
504
+ const rings = [];
505
+ for (const ring of polygonCoords) {
506
+ const latlngRing = [];
507
+ for (const coord of ring) {
508
+ if (!Array.isArray(coord) || coord.length < 2) {
509
+ continue;
510
+ }
511
+ const [lon, lat] = coord;
512
+ latlngRing.push([lat, lon]);
513
+ }
514
+ if (latlngRing.length > 0) {
515
+ // Fermer l'anneau si nécessaire (Leaflet accepte ouvert/fermé, on nettoie par sécurité)
516
+ const first = latlngRing[0];
517
+ const last = latlngRing[latlngRing.length - 1];
518
+ if (first[0] !== last[0] || first[1] !== last[1]) {
519
+ latlngRing.push([first[0], first[1]]);
520
+ }
521
+ rings.push(latlngRing);
522
+ }
523
+ }
524
+ return rings;
525
+ }
526
+
478
527
 
479
528
  // Test "sur le segment" en degrés (plan local) — suffisant pour le bord du polygone
480
529
  static isPointOnSegmentDeg(lat, lon, A, B, EPS = 1e-12) {
@@ -545,6 +594,7 @@ class Polygon {
545
594
 
546
595
  return true;
547
596
  }
597
+
548
598
  }
549
599
 
550
600
  class GeographicCoordinates {
@@ -22,11 +22,6 @@ class OpenStreetMap {
22
22
  this.locations = [];
23
23
 
24
24
  this.map = OpenStreetMap.createMap(mapContainer, options);
25
- if (null === this.map) {
26
- return;
27
- }
28
-
29
- this.centerOnFrance();
30
25
  }
31
26
 
32
27
  static createMap(mapContainer, options={}) {
@@ -82,7 +77,7 @@ class OpenStreetMap {
82
77
  listLocations.forEach(location => this.addMarker(location, icon));
83
78
  }
84
79
 
85
- addMarker(coordinates, options) {
80
+ addMarker(geoJson, options) {
86
81
  let locationCoordinates = coordinates.split(',');
87
82
  let latitude = parseFloat(locationCoordinates[0]);
88
83
  let longitude = parseFloat(locationCoordinates[1]);
@@ -110,6 +105,111 @@ class OpenStreetMap {
110
105
  this.locations.push([latitude, longitude]);
111
106
  }
112
107
 
108
+ addGeoJson(geoJson, options) {
109
+ OpenStreetMap.displayGeoJSONOnMap(this.map, geoJson, options);
110
+ }
111
+
112
+ /**
113
+ * Affiche un GeoJSON (string) sur une carte Leaflet (OSM).
114
+ * Gère: Point, Polygon, et Feature/FeatureCollection contenant ces types.
115
+ *
116
+ * @param {L.Map} map - instance Leaflet déjà créée
117
+ * @param {string} geoJson - GeoJSON en texte
118
+ * @param {object} [opts]
119
+ * - layerGroup: L.LayerGroup | L.FeatureGroup (facultatif, cible d'ajout)
120
+ * - pointMarker: options Leaflet pour le marker (ex: { draggable:false })
121
+ * - polygonStyle: options Leaflet pour le polygon (ex: { color:'#007bff' })
122
+ * - fit: boolean (par défaut true) → adapte la vue à la géométrie
123
+ * - pointZoom: number (zoom si Point et fit=true, défaut 16)
124
+ * - popup: string (facultatif) → bindPopup commun pour l'entité
125
+ * @returns {L.Layer | null} couche ajoutée (marker ou polygon), sinon null
126
+ */
127
+ static displayGeoJSONOnMap(map, geoJson, opts = {}) {
128
+ if (!map) {
129
+ console.warn('displayGeoJSONOnMap: paramètres invalides');
130
+ return null;
131
+ }
132
+
133
+ if (typeof geoJson == 'string') {
134
+ geoJson = JSON.parse(geoJson);
135
+ }
136
+
137
+ const {
138
+ layerGroup = null,
139
+ polygonStyle = { color: '#3388ff', weight: 3, opacity: 0.9, fillOpacity: 0.2 },
140
+ fit = true,
141
+ pointZoom = 16,
142
+ icon = null,
143
+ title = null,
144
+ popup = null
145
+ } = opts;
146
+
147
+ function addLayer(layer) {
148
+ layer.on('popupopen', () => {
149
+ //console.log('popupopen');
150
+ if (typeof opts['on_click'] == 'function') {
151
+ opts['on_click']();
152
+ }
153
+ return false;
154
+ });
155
+
156
+ if (layerGroup && typeof layerGroup.addLayer === 'function') {
157
+ layerGroup.addLayer(layer);
158
+ }
159
+ else {
160
+ layer.addTo(map);
161
+ }
162
+ if (popup) {
163
+ layer.bindPopup(popup);
164
+ }
165
+ return layer;
166
+ }
167
+
168
+ if (geoJson.type === 'Point') {
169
+ const coords = geoJson.coordinates;
170
+ if (!Array.isArray(coords) || coords.length < 2) {
171
+ console.warn('displayGeoJSONOnMap: Point invalide');
172
+ return null;
173
+ }
174
+ const [lon, lat] = coords;
175
+ const marker = L.marker([lat, lon], {
176
+ icon: L.icon({
177
+ iconUrl: icon,
178
+ iconSize: [22, 32],
179
+ }),
180
+ title: title,
181
+ });
182
+ addLayer(marker);
183
+
184
+ if (fit) {
185
+ map.setView([lat, lon], pointZoom);
186
+ setTimeout(function() { map.invalidateSize(true); }, 30);
187
+ }
188
+ return marker;
189
+ }
190
+
191
+ if (geoJson.type === 'Polygon') {
192
+ const rings = Polygon.toLatLngRings(geoJson.coordinates);
193
+ if (!rings.length) {
194
+ console.warn('displayGeoJSONOnMap: Polygon invalide');
195
+ return null;
196
+ }
197
+ // Leaflet accepte un tableau d’anneaux pour L.polygon
198
+ const poly = L.polygon(rings, polygonStyle);
199
+ addLayer(poly);
200
+
201
+ if (fit) {
202
+ const b = poly.getBounds();
203
+ map.fitBounds(b, { padding: [20, 20] });
204
+ setTimeout(function() { map.invalidateSize(true); }, 30);
205
+ }
206
+ return poly;
207
+ }
208
+
209
+ console.warn('displayGeoJSONOnMap: type non géré');
210
+ return null;
211
+ }
212
+
113
213
  setView(location, zoom) {
114
214
  this.map.setView(location, zoom);
115
215
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.4.12",
3
+ "version": "1.4.14",
4
4
  "main": "main.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"