Skip to content

Commit

Permalink
Fix edit geojson shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Mar 28, 2023
1 parent f8c3210 commit cd00192
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
47 changes: 32 additions & 15 deletions src/components/Map/VMap.global.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ const emit = defineEmits([
'geojson:ready',
'geojson',
'add:layer',
'draw:start'
'draw:start',
'edit:layer',
'drag:layer'
])
let mapObject
Expand Down Expand Up @@ -167,21 +169,25 @@ onMounted(() => {
})
mapObject.on('pm:create', (e) => {
const fg = L.featureGroup()
drawnItems.eachLayer((layer) => {
if (
(layer instanceof L.Path || layer instanceof L.Marker) &&
layer.pm
) {
fg.addLayer(layer)
}
})
emit('geojson', fg.toGeoJSON())
emit('geojson', getDrawItemsInGeoJson())
emit('add:layer', convertGeoJSONWithPointRadius(e.layer))
})
/* drawnItems.on('pm:change', (e) => {
emit('geojson', getDrawItemsInGeoJson())
emit('edit:layer', convertGeoJSONWithPointRadius(e.layer))
}) */
drawnItems.on('pm:edit', (e) => {
emit('geojson', getDrawItemsInGeoJson())
emit('edit:layer', convertGeoJSONWithPointRadius(e.layer))
})
drawnItems.on('pm:drag', (e) => {
emit('geojson', getDrawItemsInGeoJson())
emit('drag:layer', convertGeoJSONWithPointRadius(e.layer))
})
mapObject.on('pm:drawstart', (e) => {
clearDrawLayers()
emit('draw:start', e)
Expand All @@ -192,6 +198,18 @@ onMounted(() => {
initEvents()
})
function getDrawItemsInGeoJson() {
const fg = L.featureGroup()
drawnItems.eachLayer((layer) => {
if ((layer instanceof L.Path || layer instanceof L.Marker) && layer.pm) {
fg.addLayer(layer)
}
})
return fg.toGeoJSON()
}
function clearDrawLayers() {
drawnItems.clearLayers()
}
Expand Down Expand Up @@ -235,8 +253,7 @@ function setGeoJSON(geojson) {
if (geojson) {
L.geoJSON(geojson, {
...geojsonDefaultOptions,
...props.geojsonOptions,
pmIgnore: true
...props.geojsonOptions
}).addTo(geoJSONGroup)
const bounds = geoJSONGroup.getBounds()
Expand Down
16 changes: 15 additions & 1 deletion src/components/Map/utils/geojsonOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ const TYPES = [
'Georeference'
]

const DEFAULT_OPTIONS = {
allowEditing: false,
allowRemoval: false,
allowCutting: false,
allowRotation: false,
draggable: false
}

function getRelevantType(base) {
const types = base.map((b) => b.type)

Expand Down Expand Up @@ -37,13 +45,19 @@ export default {
.join('')}
</ul></div>`

layer.pm.setOptions(DEFAULT_OPTIONS)
layer.pm.disable()

layer.bindPopup(label)
},

pointToLayer: (feature, latLng) => {
const type = getRelevantType(feature.properties.base)
const marker = L.marker(latLng, { icon: Icons[type] || Icon.Georeference })

marker.pm.setOptions(DEFAULT_OPTIONS)

return L.marker(latLng, { icon: Icons[type] || Icon.Georeference })
return marker
},

style: (feature) => {
Expand Down
17 changes: 9 additions & 8 deletions src/modules/otus/components/Search/OtuSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
:disable-zoom="!!shapes"
:zoom-bounds="6"
:geojson="shapes"
@add:layer="setLayer"
@edit:layer="setLayer"
@drag:layer="setLayer"
@draw:start="() => (geojson = {})"
@add:layer="
($event) => {
geoJson = JSON.stringify($event.geometry)
}
"
:zoom="4"
/>
<div
Expand All @@ -46,7 +44,7 @@

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { makeAPIRequest } from '@/utils/request'
import TaxonWorks from '../../services/TaxonWorks'
import SearchBar from './SearchBar.vue'
import ListResults from './ListResults.vue'
Expand Down Expand Up @@ -88,6 +86,10 @@ const handleKeyboard = ({ key }) => {
}
}
function setLayer(geojsonLayer) {
geoJson.value = JSON.stringify(geojsonLayer.geometry)
}
function loadOTUs() {
const payload = {
geo_json: geoJson.value,
Expand All @@ -97,8 +99,7 @@ function loadOTUs() {
isLoading.value = true
makeAPIRequest
.get('/otus.json', { params: payload })
TaxonWorks.getOtus(payload)
.then(({ data }) => {
list.value = data
isTableVisible.value = true
Expand Down
6 changes: 6 additions & 0 deletions src/modules/otus/services/TaxonWorks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default class TaxonWorks {
})
}

static getOtus(params) {
return makeAPIRequest.get(`/otus.json`, {
params
})
}

static getTaxon(id) {
return makeAPIRequest.get(`/taxon_names/${id}`)
}
Expand Down

0 comments on commit cd00192

Please sign in to comment.