Skip to content

Commit

Permalink
refactor: replace unecessary outline_centroid for tasks with ol.exten…
Browse files Browse the repository at this point in the history
…t.getCenter (#1447)
  • Loading branch information
spwoodcock authored Apr 15, 2024
1 parent 89fa574 commit ae7adde
Show file tree
Hide file tree
Showing 10 changed files with 6 additions and 81 deletions.
18 changes: 1 addition & 17 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from pydantic.functional_serializers import field_serializer
from pydantic.functional_validators import field_validator

from app.db.postgis_utils import geometry_to_geojson, get_centroid
from app.db.postgis_utils import geometry_to_geojson
from app.models.enums import TaskStatus


Expand Down Expand Up @@ -70,7 +70,6 @@ class Task(BaseModel):
project_task_index: int
project_task_name: Optional[str]
outline_geojson: Optional[GeojsonFeature] = None
outline_centroid: Optional[GeojsonFeature] = None
feature_count: Optional[int] = None
task_status: TaskStatus
locked_by_uid: Optional[int] = None
Expand All @@ -90,21 +89,6 @@ def get_geojson_from_outline(cls, value: Any, info: ValidationInfo) -> str:
return geometry_to_geojson(outline, properties, info.data.get("id"))
return None

@field_validator("outline_centroid", mode="before")
@classmethod
def get_centroid_from_outline(
cls, value: Any, info: ValidationInfo
) -> Optional[str]:
"""Get outline_centroid from Shapely geom."""
if outline := info.data.get("outline"):
properties = {
"fid": info.data.get("project_task_index"),
"uid": info.data.get("id"),
"name": info.data.get("project_task_name"),
}
return get_centroid(outline, properties, info.data.get("id"))
return None

@field_serializer("locked_by_uid")
def get_locked_by_uid(self, value: str) -> Optional[str]:
"""Get lock uid from lock_holder details."""
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/api/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const ProjectById = (existingProjectList, projectId) => {
return {
id: data.id,
outline_geojson: data.outline_geojson,
outline_centroid: data.outline_centroid,
task_status: task_priority_str[data.task_status],
locked_by_uid: data.locked_by_uid,
locked_by_username: data.locked_by_username,
Expand Down
3 changes: 0 additions & 3 deletions src/frontend/src/api/ProjectTaskStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ const UpdateTaskStatus = (url, style, existingData, currentProjectId, feature, m
}
};
await updateTask(url, existingData, body, feature, params);
const centroid = await existingData[index].taskBoundries.filter((task) => {
return task.id == taskId;
})[0].outline_centroid.geometry.coordinates;
};
};

Expand Down
28 changes: 0 additions & 28 deletions src/frontend/src/components/Activities.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,6 @@ const Activities = ({ history, defaultTheme, mapDivPostion, map, view, state, pa
<CoreModules.Typography variant="h2" style={{ wordWrap: 'break-word' }}>
{history.action_text}
</CoreModules.Typography>
{/* <CoreModules.Stack direction={'row-reverse'}>
<IconButtonCard
element={
<CoreModules.IconButton
onClick={async () => {
const main = document.getElementsByClassName('mainview')[0];
await main.scrollTo({
top: mapDivPostion,
});
const centroid = state.projectTaskBoundries[index].taskBoundries.filter((task) => {
return task.id == history.taskId;
})[0].outline_centroid.geometry.coordinates;
map.getView().setCenter(centroid);
setTimeout(() => {
view.animate({ zoom: 19, easing: easeOut, duration: 2000 });
}, 100);
}}
color="info"
aria-label="share qrcode"
>
<AssetModules.LinkIcon color="info" sx={{ fontSize: defaultTheme.typography.fontSize }} />
</CoreModules.IconButton>
}
/>
</CoreModules.Stack> */}
</CoreModules.Stack>

<CoreModules.Divider color="lightgray" />
Expand Down
8 changes: 2 additions & 6 deletions src/frontend/src/hooks/MapStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import CoreModules from '@/shared/CoreModules';
import AssetModules from '@/shared/AssetModules';
import { getCenter } from 'ol/extent';
import Point from 'ol/geom/Point.js';
import { transform } from 'ol/proj';

function createPolygonStyle(fillColor, strokeColor) {
return new Style({
Expand All @@ -29,11 +28,8 @@ function createIconStyle(iconSrc) {
src: iconSrc,
}),
geometry: function (feature) {
// return the coordinates of the centroid of the polygon
// const coordinates = feature.getGeometry().getExtent();
// const center = getCenter(coordinates);
const convertedCenter = transform(feature.values_.centroid, 'EPSG:4326', 'EPSG:3857');
return new Point(convertedCenter);
const polygonCentroid = getCenter(feature.getGeometry().getExtent());
return new Point(polygonCentroid);
},
});
}
Expand Down
10 changes: 0 additions & 10 deletions src/frontend/src/models/createproject/createProjectModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ export interface ProjectDetailsModel {
id: string;
bbox: [string, string, string, string];
};
outline_centroid: {
type: string;
geometry: {
coordinates: [string, string];
type: string;
};
properties: Record<string, any>;
id: string;
bbox: [string, string, string, string];
};
task_status: number;
locked_by_uid: number;
locked_by_username: string;
Expand Down
10 changes: 0 additions & 10 deletions src/frontend/src/models/project/projectModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,6 @@ export type taskBoundriesTypes = {
id: string;
bbox: [string, string, string, string];
};
outline_centroid: {
type: string;
geometry: {
coordinates: [string, string];
type: string;
};
properties: Record<string, any>;
id: string;
bbox: [string, string, string, string];
};
task_history: taskHistoryTypes[];
task_status: string;
};
1 change: 0 additions & 1 deletion src/frontend/src/store/types/ICreateProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export type ProjectTaskTypes = {
project_id: number;
project_task_index: number;
outline_geojson: GeoJSONFeatureTypes;
outline_centroid: GeoJSONFeatureTypes;
task_status: number;
locked_by_uid: number | null;
locked_by_username: string | null;
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/src/utilfunctions/getTaskStatusStyle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Fill, Icon, Stroke, Style } from 'ol/style';
import { transform } from 'ol/proj';
import { getCenter } from 'ol/extent';
import { Point } from 'ol/geom';
import AssetModules from '@/shared/AssetModules';
import { task_priority_str } from '@/types/enums';
Expand All @@ -25,8 +25,8 @@ function createIconStyle(iconSrc) {
src: iconSrc,
}),
geometry: function (feature) {
const convertedCenter = transform(feature.values_.centroid, 'EPSG:4326', 'EPSG:3857');
return new Point(convertedCenter);
const polygonCentroid = getCenter(feature.getGeometry().getExtent());
return new Point(polygonCentroid);
},
});
}
Expand Down
2 changes: 0 additions & 2 deletions src/frontend/src/views/ProjectDetailsV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ const Home = () => {
geometry: { ...taskObj.outline_geojson.geometry },
properties: {
...taskObj.outline_geojson.properties,
centroid: taskObj.outline_centroid.geometry.coordinates,
// TODO add bbox field here too?
},
id: `${taskObj.id}_${taskObj.task_status}`,
}));
Expand Down

0 comments on commit ae7adde

Please sign in to comment.