Skip to content

Commit

Permalink
feat: soil ID algorithm service (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouxm authored May 14, 2024
1 parent 162876d commit d866588
Show file tree
Hide file tree
Showing 8 changed files with 484 additions and 305 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lodash": "^4.17.21",
"react": "^18.3.1",
"react-redux": "^8.1.3",
"terraso-backend": "github:techmatters/terraso-backend#edac5d6",
"terraso-backend": "github:techmatters/terraso-backend#fe4cbf8",
"uuid": "^9.0.1"
},
"scripts": {
Expand Down
122 changes: 122 additions & 0 deletions src/soilId/soilDataFragments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

export const projectSoilSettings = /* GraphQL */ `
fragment projectSoilSettings on ProjectSoilSettingsNode {
depthIntervals {
depthInterval {
start
end
}
label
}
depthIntervalPreset
soilPitRequired
slopeRequired
soilTextureRequired
soilColorRequired
verticalCrackingRequired
carbonatesRequired
phRequired
soilOrganicCarbonMatterRequired
electricalConductivityRequired
sodiumAdsorptionRatioRequired
soilStructureRequired
landUseLandCoverRequired
soilLimitationsRequired
photosRequired
notesRequired
}
`;

export const soilData = /* GraphQL */ `
fragment soilData on SoilDataNode {
downSlope
crossSlope
bedrock
depthIntervalPreset
slopeLandscapePosition
slopeAspect
slopeSteepnessSelect
slopeSteepnessPercent
slopeSteepnessDegree
surfaceCracksSelect
surfaceSaltSelect
floodingSelect
limeRequirementsSelect
surfaceStoninessSelect
waterTableDepthSelect
soilDepthSelect
landCoverSelect
grazingSelect
depthIntervals {
...soilDataDepthInterval
}
depthDependentData {
...depthDependentSoilData
}
}
`;

export const soilDataDepthInterval = /* GraphQL */ `
fragment soilDataDepthInterval on SoilDataDepthIntervalNode {
label
depthInterval {
start
end
}
soilTextureEnabled
soilColorEnabled
carbonatesEnabled
phEnabled
soilOrganicCarbonMatterEnabled
electricalConductivityEnabled
sodiumAdsorptionRatioEnabled
soilStructureEnabled
}
`;

export const depthDependentSoilData = /* GraphQL */ `
fragment depthDependentSoilData on DepthDependentSoilDataNode {
depthInterval {
start
end
}
texture
rockFragmentVolume
clayPercent
colorHue
colorValue
colorChroma
colorPhotoUsed
colorPhotoSoilCondition
colorPhotoLightingCondition
conductivity
conductivityTest
conductivityUnit
structure
ph
phTestingSolution
phTestingMethod
soilOrganicCarbon
soilOrganicMatter
soilOrganicCarbonTesting
soilOrganicMatterTesting
sodiumAbsorptionRatio
carbonates
}
`;
238 changes: 238 additions & 0 deletions src/soilId/soilDataService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
/*
* Copyright © 2024 Technology Matters
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/

import { graphql } from 'terraso-client-shared/graphqlSchema';
import type {
DepthDependentSoilDataUpdateMutationInput,
ProjectSoilSettingsDeleteDepthIntervalMutationInput,
ProjectSoilSettingsUpdateDepthIntervalMutationInput,
ProjectSoilSettingsUpdateMutationInput,
SoilDataDeleteDepthIntervalMutationInput,
SoilDataUpdateDepthIntervalMutationInput,
SoilDataUpdateMutationInput,
} from 'terraso-client-shared/graphqlSchema/graphql';
import { collapseProjects } from 'terraso-client-shared/project/projectService';
import { collapseSites } from 'terraso-client-shared/site/siteService';
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api';
import {
collapseEdges,
collapseMaps,
} from 'terraso-client-shared/terrasoApi/utils';

export const fetchSoilDataForUser = async (userId: string) => {
const query = graphql(`
query userSoilData($id: ID!) {
userSites: sites(owner: $id) {
edges {
node {
...siteData
soilData {
...soilData
}
}
}
}
projects: projects(member: $id) {
edges {
node {
...projectData
siteSet {
edges {
node {
soilData {
...soilData
}
}
}
}
soilSettings {
...projectSoilSettings
}
}
}
}
}
`);

const { userSites, projects: allProjects } = await terrasoApi.requestGraphQL(
query,
{ id: userId },
);

const {
projects,
sites: projectSites,
users,
} = collapseProjects(allProjects);
const allSites = collapseEdges(userSites).concat(
collapseEdges(allProjects).flatMap(({ siteSet }) => collapseEdges(siteSet)),
);

return {
projects,
users,
projectSoilSettings: Object.fromEntries(
collapseEdges(allProjects).map(({ soilSettings, id }) => [
id,
soilSettings,
]),
),
sites: collapseMaps(collapseSites(userSites), projectSites),
soilData: Object.fromEntries(
allSites.map(({ soilData, id }) => [id, soilData]),
),
};
};

export const updateSoilData = async (soilData: SoilDataUpdateMutationInput) => {
const query = graphql(`
mutation updateSoilData($input: SoilDataUpdateMutationInput!) {
updateSoilData(input: $input) {
soilData {
...soilData
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, { input: soilData });
return resp.updateSoilData.soilData!;
};

export const updateDepthDependentSoilData = async (
depthDependentData: DepthDependentSoilDataUpdateMutationInput,
) => {
const query = graphql(`
mutation updateDepthDependentSoilData(
$input: DepthDependentSoilDataUpdateMutationInput!
) {
updateDepthDependentSoilData(input: $input) {
soilData {
...soilData
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, {
input: depthDependentData,
});

return resp.updateDepthDependentSoilData.soilData!;
};

export const updateSoilDataDepthInterval = async (
soilData: SoilDataUpdateDepthIntervalMutationInput,
) => {
const query = graphql(`
mutation updateSoilDataDepthInterval(
$input: SoilDataUpdateDepthIntervalMutationInput!
) {
updateSoilDataDepthInterval(input: $input) {
soilData {
...soilData
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, { input: soilData });
return resp.updateSoilDataDepthInterval.soilData!;
};

export const deleteSoilDataDepthInterval = async (
soilData: SoilDataDeleteDepthIntervalMutationInput,
) => {
const query = graphql(`
mutation deleteSoilDataDepthInterval(
$input: SoilDataDeleteDepthIntervalMutationInput!
) {
deleteSoilDataDepthInterval(input: $input) {
soilData {
...soilData
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, { input: soilData });
return resp.deleteSoilDataDepthInterval.soilData!;
};

export const updateProjectSoilSettings = async (
soilSettings: ProjectSoilSettingsUpdateMutationInput,
) => {
const query = graphql(`
mutation updateProjectSoilSettings(
$input: ProjectSoilSettingsUpdateMutationInput!
) {
updateProjectSoilSettings(input: $input) {
projectSoilSettings {
...projectSoilSettings
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, { input: soilSettings });
return resp.updateProjectSoilSettings.projectSoilSettings!;
};

export const updateProjectDepthInterval = async (
depthInterval: ProjectSoilSettingsUpdateDepthIntervalMutationInput,
) => {
const query = graphql(`
mutation updateProjectSoilSettingsDepthInterval(
$input: ProjectSoilSettingsUpdateDepthIntervalMutationInput!
) {
updateProjectSoilSettingsDepthInterval(input: $input) {
projectSoilSettings {
...projectSoilSettings
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, { input: depthInterval });
return resp.updateProjectSoilSettingsDepthInterval.projectSoilSettings!;
};

export const deleteProjectDepthInterval = async (
depthInterval: ProjectSoilSettingsDeleteDepthIntervalMutationInput,
) => {
const query = graphql(`
mutation deleteProjectSoilSettingsDepthInterval(
$input: ProjectSoilSettingsDeleteDepthIntervalMutationInput!
) {
deleteProjectSoilSettingsDepthInterval(input: $input) {
projectSoilSettings {
...projectSoilSettings
}
errors
}
}
`);

const resp = await terrasoApi.requestGraphQL(query, { input: depthInterval });
return resp.deleteProjectSoilSettingsDepthInterval.projectSoilSettings!;
};
Loading

0 comments on commit d866588

Please sign in to comment.