Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-native geometries not visible in map layout #10308

Merged
merged 2 commits into from Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/src/interfaces/map/map.vue
Expand Up @@ -257,7 +257,7 @@ export default defineComponent({
...(mapboxKey ? { accessToken: mapboxKey } : {}),
});
if (controls.geocoder) {
map.addControl(controls.geocoder, 'top-right');
map.addControl(controls.geocoder as any, 'top-right');
controls.geocoder.on('result', (event: any) => {
location.value = event.result.center;
});
Expand Down Expand Up @@ -336,7 +336,8 @@ export default defineComponent({

function fitDataBounds(options: CameraOptions & AnimationOptions) {
if (map && currentGeometry) {
map.fitBounds(currentGeometry.bbox! as LngLatBoundsLike, {
const bbox = getBBox(currentGeometry);
map.fitBounds(bbox as LngLatBoundsLike, {
padding: 80,
maxZoom: 8,
...options,
Expand Down Expand Up @@ -432,7 +433,6 @@ export default defineComponent({
} else {
result = geometries[geometries.length - 1];
}
result!.bbox = getBBox(result!);
return result;
}

Expand Down
19 changes: 4 additions & 15 deletions app/src/layouts/map/index.ts
Expand Up @@ -6,7 +6,7 @@ import MapActions from './actions.vue';
import { useI18n } from 'vue-i18n';
import { toRefs, computed, ref, watch } from 'vue';

import { toGeoJSON } from '@/utils/geometry';
import { toGeoJSON, getGeometryFormatForType } from '@/utils/geometry';
import { layers as directusLayers } from './style';
import { useRouter } from 'vue-router';
import { useSync } from '@directus/shared/composables';
Expand Down Expand Up @@ -64,23 +64,11 @@ export default defineLayout<LayoutOptions, LayoutQuery>({
const cameraOptions = syncRefProperty(layoutOptions, 'cameraOptions', undefined);
const clusterData = syncRefProperty(layoutOptions, 'clusterData', false);
const geometryField = syncRefProperty(layoutOptions, 'geometryField', undefined);
const geometryFormat = computed<GeometryFormat | undefined>({
get: () => layoutOptions.value?.geometryFormat,
set(newValue: GeometryFormat | undefined) {
layoutOptions.value = {
...(layoutOptions.value || {}),
geometryFormat: newValue,
geometryField: undefined,
};
},
});

const geometryFieldData = computed(() => {
return fieldsInCollection.value.find((f: Field) => f.field == geometryField.value);
});

const isGeometryFieldNative = computed(() => geometryFieldData.value?.type.startsWith('geometry'));

const geometryFields = computed(() => {
return (fieldsInCollection.value as Field[]).filter(
({ type, meta }) => type.startsWith('geometry') || meta?.interface == 'map'
Expand All @@ -103,14 +91,16 @@ export default defineLayout<LayoutOptions, LayoutQuery>({
return;
}
const geometryField = field.field;
const geometryFormat = isGeometryFieldNative.value ? 'native' : field.meta?.options?.geometryFormat;
const geometryFormat = getGeometryFormatForType(field.type);
const geometryType = field.type.split('.')[1] ?? field.meta?.options?.geometryType;
if (!geometryFormat) {
return;
}
return { geometryField, geometryFormat, geometryType };
});

const isGeometryFieldNative = computed(() => geometryOptions.value?.geometryFormat === 'native');

const template = computed(() => {
return displayTemplate.value || info.value?.meta?.display_template || `{{ ${primaryKeyField.value?.field} }}`;
});
Expand Down Expand Up @@ -293,7 +283,6 @@ export default defineLayout<LayoutOptions, LayoutQuery>({
geometryOptions,
handleClick,
handleSelect,
geometryFormat,
geometryField,
displayTemplate,
isGeometryFieldNative,
Expand Down
2 changes: 0 additions & 2 deletions app/src/layouts/map/types.ts
@@ -1,5 +1,4 @@
import { CameraOptions } from 'maplibre-gl';
import { GeometryFormat } from '@directus/shared/types';

export type LayoutQuery = {
fields: string[];
Expand All @@ -10,7 +9,6 @@ export type LayoutQuery = {

export type LayoutOptions = {
cameraOptions?: CameraOptions & { bbox: any };
geometryFormat?: GeometryFormat;
geometryField?: string;
autoLocationFilter?: boolean;
clusterData?: boolean;
Expand Down
3 changes: 1 addition & 2 deletions app/src/utils/geometry/index.ts
Expand Up @@ -82,7 +82,6 @@ export function getParser(options: GeometryOptions): GeoJSONParser {
const geomRaw = entry[options.geometryField];
const geom = geomRaw && parse(geomRaw);
if (!geom) return undefined;
geom.bbox = getBBox(geom);
return geom;
};
}
Expand All @@ -99,7 +98,7 @@ export function toGeoJSON(entries: any[], options: GeometryOptions): FeatureColl
for (let i = 0; i < entries.length; i++) {
const geometry = parser(entries[i]);
if (!geometry) continue;
const [a, b, c, d] = geometry.bbox!;
const [a, b, c, d] = getBBox(geometry);
geojson.bbox = expandBBox(geojson.bbox!, [a, b]);
geojson.bbox = expandBBox(geojson.bbox!, [c, d]);
const properties = { ...entries[i] };
Expand Down