Skip to content

Commit

Permalink
Merge pull request #159 from developmentseed/fix/geojson-upload
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge authored Apr 18, 2024
2 parents a08d1f1 + c2bdf42 commit b7e316e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
18 changes: 11 additions & 7 deletions app/assets/scripts/fsm/project/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,15 +638,8 @@ export const actions = {
rectangleAoi.shape.remove();
}

const isFirstAoi = context.aoisList.length === 0;

return {
rectangleAoi: null,
aoiActionButtons: {
uploadAoi: true,
addNewAoi: !isFirstAoi,
deleteAoi: true,
},
mapEventHandlers: {
dragging: true,
mousedown: false,
Expand All @@ -655,6 +648,17 @@ export const actions = {
},
};
}),
exitDefiningAoiBounds: assign((context) => {
const isFirstAoi = context.aoisList.length === 0;

return {
aoiActionButtons: {
uploadAoi: true,
addNewAoi: !isFirstAoi,
deleteAoi: true,
},
};
}),
enterConfiguringNewAoi: assign(() => {
return {
currentTimeframe: null,
Expand Down
2 changes: 1 addition & 1 deletion app/assets/scripts/fsm/project/machine.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export const projectMachine = createMachine(
src: 'geocodeAoi',
onDone: {
target: 'Configuring new AOI',
actions: 'setCurrentAoiName',
actions: ['setCurrentAoiName', 'exitDefiningAoiBounds'],
},
},
},
Expand Down
38 changes: 18 additions & 20 deletions app/assets/scripts/utils/reverse-geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,27 @@ export async function reverseGeocodeLatLng(lat, lng) {
let name = 'Area';

try {
const address = await fetch(
`${config.bingSearchUrl}/Locations/${lat},${lng}?radius=${config.reverseGeocodeRadius}&includeEntityTypes=address,AdminDivision1,AdminDivision2, CountryRegion&key=${config.bingApiKey}`,
{
headers: {
'Content-Type': 'application/json',
},
mode: 'cors',
}
).then((res) => res.json());
const url = `${config.bingSearchUrl}/Locations/${lat},${lng}?radius=${config.reverseGeocodeRadius}&includeEntityTypes=address,AdminDivision1,AdminDivision2,CountryRegion&key=${config.bingApiKey}`;
const response = await fetch(url, {
headers: { 'Content-Type': 'application/json' },
mode: 'cors',
});

if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

const address = await response.json();

if (address && address.resourceSets[0].estimatedTotal) {
if (address && address.resourceSets[0].estimatedTotal > 0) {
const result = address.resourceSets[0].resources[0];
const { entityType } = result;

switch (entityType) {
case 'Address':
name = result.address.locality;
break;
case 'AdminDivision1':
case 'AdminDivision2':
case 'CountryRegion':
name = result.name;
}
name =
result.address?.locality ||
result.address?.adminDistrict2 ||
result.address?.adminDistrict ||
result.address?.countryRegion ||
'Area';
}
} catch (error) {
logger(error);
Expand Down

0 comments on commit b7e316e

Please sign in to comment.