MCPcopy Create free account
hub / github.com/Thanas-R/Virdis / detect

Function detect

src/components/NewFieldDialog.tsx:95–133  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

93
94 // Auto-detect region type using GEE land use + reverse geocode (server-proxied)
95 useEffect(() => {
96 const detect = async () => {
97 setDetecting(true);
98
99 const center = coordinates.reduce(
100 (acc, c) => [acc[0] + c[0] / coordinates.length, acc[1] + c[1] / coordinates.length], [0, 0]
101 );
102
103 // Parallel: reverse geocode (via edge proxy) + GEE land use
104 const geocodePromise = callBackend("mapbox-geocode", { mode: "reverse", lng: center[0], lat: center[1], limit: 1 })
105 .then(r => r.data).catch(() => null);
106
107 const geePromise = callBackend("gee-analytics", { polygon: coordinates, analyses: ["land_use"] }).then(r => r.data).catch(() => null);
108
109 const [geoData, geeData] = await Promise.all([geocodePromise, geePromise]);
110
111 // Set location from geocode
112 if (geoData?.features?.[0]) {
113 setLocation(geoData.features[0].place_name);
114 }
115
116 // Determine region type from GEE land use data
117 let isUrban = false;
118 if (geeData?.land_use) {
119 const builtUp = geeData.land_use["Built-up"] || 0;
120 isUrban = builtUp >= 30;
121 } else if (geoData?.features?.[0]) {
122 // Fallback: check Mapbox place type
123 const placeType = geoData.features[0].place_type?.[0] || "";
124 const placeName = (geoData.features[0].place_name || "").toLowerCase();
125 isUrban = ["place", "locality", "neighborhood", "address"].includes(placeType) ||
126 ["city", "town", "metro", "urban", "suburb", "downtown"].some(k => placeName.includes(k));
127 }
128
129 setRegionType(isUrban ? "urban" : "rural");
130 setDetecting(false);
131
132 if (!isUrban) {
133 setGuessingCrop(true);
134 try {
135 const cropNames = CROP_OPTIONS.map(c => c.name);
136 const { data } = await callBackend<{ crop?: string }>("guess-crop", {

Callers 1

NewFieldDialogFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected