dolibarr 21.0.0-alpha
geomapeditor.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program 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
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
28{
34 public function __construct()
35 {
36 }
37
48 public function getHtml($htmlname, $geojson, $centroidjson, $markertype)
49 {
50 global $langs;
51
52 $out = '<input id="' . $htmlname . '" name="' . $htmlname . '" size="100" value="' . htmlentities($geojson, ENT_QUOTES) . '"/>';
53 $out .= '<div id="map_' . $htmlname . '" style="width: 600px; height: 350px;"></div>';
54 if ($geojson != '{}') {
55 // OpenLayers it's "longitude, latitude".
56 // inverting coordinates
57 $tmp = json_decode($geojson);
58 $tmp2 = new stdClass();
59 $tmp2->type = $tmp->type;
60 $tmp2->coordinates = [];
61 if ($tmp->type == 'Point') {
62 $tmp2->coordinates = [$tmp->coordinates[1], $tmp->coordinates[0]];
63 } elseif ($tmp->type == 'Polygon') {
64 foreach ($tmp->coordinates as $polygon) {
65 $polyg = [];
66 foreach ($polygon as $key => $value) {
67 $polyg[] = [$value[1], $value[0]];
68 }
69 $tmp2->coordinates[] = $polyg;
70 }
71 } else {
72 foreach ($tmp->coordinates as $key => $value) {
73 $tmp2->coordinates[] = [$value[1], $value[0]];
74 }
75 }
76 $geojson = json_encode($tmp2);
77 }
78 if ($centroidjson != '{}') {
79 if (null === json_decode($centroidjson)) {
80 $centroidjson = '{}';
81 } else {
82 // OpenLayers it's "longitude, latitude".
83 // inverting coordinates
84 $tmp = json_decode($centroidjson);
85 $tmp2 = new stdClass();
86 $tmp2->type = $tmp->type;
87 $tmp2->coordinates = [];
88 if ($tmp->type == 'Point') {
89 $tmp2->coordinates = [$tmp->coordinates[1], $tmp->coordinates[0]];
90 } else {
91 foreach ($tmp->coordinates as $key => $value) {
92 $tmp2->coordinates[] = [$value[1], $value[0]];
93 }
94 }
95 $centroidjson = json_encode($tmp2);
96 }
97 }
98 $out .= '
99 <script>
100 var geoms = JSON.parse(\'' . $geojson . '\');
101 var centroid = JSON.parse(\'' . $centroidjson . '\');
102 var markerType = "' . $markertype . '";
103 var map = L.map("map_' . $htmlname . '");
104 console.log(markerType);
105 console.log(geoms);
106 map.pm.addControls({
107 position: \'topleft\',
108 dragMode: false,
109 drawMarker: false,
110 drawCircle:false,
111 drawCircleMarker: false,
112 drawText: false,
113 drawRectangle: false,
114 editMode: true,
115 removalMode: true,
116 rotateMode: false,
117 customControls: false,
118 });
119 console.log(centroid);
120 if (centroid.type == "Point") {
121 map.setView(centroid.coordinates, 11);
122 } else {
123 map.setView([48.852, 2.351], 12);
124 }
125 if (markerType == "point" && Object.keys(geoms).length != 0) {
126 //map.setView(geoms.coordinates, 17);
127 L.marker(geoms.coordinates).addTo(map);
128 // disableMarkers();
129 map.pm.addControls({
130 drawMarker: false,
131 drawPolyline: false,
132 drawPolygon: false,
133 });
134 } else if (markerType == "multipts" && Object.keys(geoms).length != 0) {
135 L.multipoint(geoms.coordinates).addTo(map);
136 } else if (markerType == "linestrg" && Object.keys(geoms).length != 0) {
137 L.polyline(geoms.coordinates).addTo(map);
138 } else if (markerType == "polygon" && Object.keys(geoms).length != 0) {
139 L.polygon(geoms.coordinates).addTo(map);
140 } else if (Object.keys(geoms).length === 0) {
141 // map.setView([48.852, 2.351], 12);
142 }
143 var tiles = L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
144 maxZoom: 19,
145 attribution: \'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>\'
146 }).addTo(map);
147 map.pm.setLang("' . ($langs->shortlang ?? 'en') . '");
148
149 enableMarker(markerType);
150 // if (geoms && geoms.type == "Point") {
151 // L.marker([geoms.coordinates[1], geoms.coordinates[0]]).addTo(map);
152 // disableMarkers();
153 // }
154 map.on("pm:drawend", (e) => {
155 disableMarkers();
156 generateGeoJson();
157 console.log("pm:drawend");
158 console.log(e);
159 });
160 map.on("pm:markerdragend", (e) => {
161 disableMarkers();
162 generateGeoJson();
163 console.log("pm:markerdragend");
164 console.log(e);
165 });
166 map.on("pm:remove", (e) => {
167 enableMarker();
168 console.log(e);
169 $("#' . $htmlname . '").val ("{}");
170 });
171 map.on("pm:edit", (e) => {
172 console.log("pm:edit");
173 generateGeoJson();
174 });
175 map.on("pm:create", (e) => {
176 console.log("pm:create");
177 generateGeoJson();
178 });
179 map.on("pm:globaleditmodetoggled", (e) => {
180 generateGeoJson();
181 console.log(e);
182 });
183 function enableMarker(type) {
184 console.log("enable : " + type);
185 if (type == "point") {
186 map.pm.addControls({
187 drawMarker: true
188 });
189 };
190 }
191 function disableMarkers(){
192 map.pm.addControls({
193 drawMarker: false,
194 drawPolyline: false,
195 drawPolygon: false,
196 });
197 }
198
199 function generateGeoJson(){
200 var fg = L.featureGroup();
201 var layers = findLayers(map);
202 layers.forEach(function(layer){
203 fg.addLayer(layer);
204 });
205 console.log(fg.toGeoJSON());
206 $("#' . $htmlname . '").val (JSON.stringify(fg.toGeoJSON().features[0].geometry));
207 }
208 function findLayers(map) {
209 // https://stackoverflow.com/questions/62887120/leafletjs-geoman-into-json-data
210 var layers = [];
211 map.eachLayer(layer => {
212 if (
213 layer instanceof L.Polyline || // Don"t worry about Polygon and Rectangle they are included in Polyline
214 layer instanceof L.Marker ||
215 layer instanceof L.Circle ||
216 layer instanceof L.CircleMarker
217 ) {
218 layers.push(layer);
219 }
220 });
221 // filter out layers that don"t have the leaflet-geoman instance
222 layers = layers.filter(layer => !!layer.pm);
223 // filter out everything that"s leaflet-geoman specific temporary stuff
224 layers = layers.filter(layer => !layer._pmTempLayer);
225 return layers;
226 }
227 </script>';
228
229 return $out;
230 }
231}
print $object position
Definition edit.php:195
Class to manage a Leaflet map width geometrics objects.
__construct()
__contruct
getHtml($htmlname, $geojson, $centroidjson, $markertype)
getHtml