-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
osmApi: refactor – break up
osmApi.ts
(#884)
- Loading branch information
Showing
20 changed files
with
235 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/FeaturePanel/EditDialog/EditContent/AddMemberForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { LonLat } from '../types'; | ||
import { publishDbgObject } from '../../utils'; | ||
|
||
/** | ||
* This holds coords of clicked ways/relations (from vector map), these are often different than those computed by us | ||
* TODO: we should probably store just the last one, but this cant get too big, right? | ||
*/ | ||
export const featureCenterCache: Record<string, LonLat> = {}; | ||
|
||
export const addFeatureCenterToCache = (shortId: string, center: LonLat) => { | ||
featureCenterCache[shortId] = center; | ||
publishDbgObject('featureCenterCache', featureCenterCache); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { OsmId } from '../types'; | ||
import { fetchJson } from '../fetch'; | ||
import { getOsmParentUrl } from './urls'; | ||
import { addSchemaToFeature } from '../tagging/idTaggingScheme'; | ||
import { osmToFeature } from './osmToFeature'; | ||
|
||
const getOsmParentPromise = async (apiId: OsmId) => { | ||
const { elements } = await fetchJson(getOsmParentUrl(apiId)); | ||
return { elements }; | ||
}; | ||
|
||
export const fetchParentFeatures = async (apiId: OsmId) => { | ||
const { elements } = await getOsmParentPromise(apiId); | ||
return elements.map((element) => addSchemaToFeature(osmToFeature(element))); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Feature, OsmId } from '../types'; | ||
import { fetchJson } from '../fetch'; | ||
import { getOsmFullUrl, getOsmUrl } from './urls'; | ||
import { addSchemaToFeature } from '../tagging/idTaggingScheme'; | ||
import { osmToFeature } from './osmToFeature'; | ||
import { getItemsMap, getMemberFeatures } from './helpers'; | ||
import { mergeMemberImageDefs } from '../images/getImageDefs'; | ||
|
||
export const fetchWithMemberFeatures = async (apiId: OsmId) => { | ||
if (apiId.type !== 'relation') { | ||
const wayOrNodeResponse = await fetchJson(getOsmUrl(apiId)); | ||
const wayOrNode = wayOrNodeResponse.elements[0]; | ||
return addSchemaToFeature(osmToFeature(wayOrNode)); | ||
} | ||
|
||
const full = await fetchJson(getOsmFullUrl(apiId)); | ||
const map = getItemsMap(full.elements); | ||
const relation = map.relation[apiId.id]; | ||
|
||
const out: Feature = { | ||
...addSchemaToFeature(osmToFeature(relation)), | ||
memberFeatures: getMemberFeatures(relation.members, map), | ||
}; | ||
mergeMemberImageDefs(out); | ||
return out; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Feature } from '../types'; | ||
import { resolveCountryCode } from 'next-codegrid'; | ||
import * as Sentry from '@sentry/nextjs'; | ||
|
||
export const getCountryCode = async ( | ||
feature: Feature, | ||
): Promise<string | null> => { | ||
try { | ||
return await resolveCountryCode(feature.center); // takes 0-100ms for first resolution, then instant | ||
} catch (e) { | ||
console.warn('countryCode left empty – resolveCountryCode():', e); // eslint-disable-line no-console | ||
Sentry.captureException(e, { extra: { feature } }); | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { OsmId } from '../types'; | ||
import { fetchSchemaTranslations } from '../tagging/translations'; | ||
import { fetchJson } from '../fetch'; | ||
import { OsmResponse } from './types'; | ||
import { getOsmUrlOrFull } from './urls'; | ||
import { getItemsMap, getMemberFeatures } from './helpers'; | ||
import { addSchemaToFeature } from '../tagging/idTaggingScheme'; | ||
import { osmToFeature } from './osmToFeature'; | ||
|
||
export const getFullFeatureWithMemberFeatures = async (apiId: OsmId) => { | ||
await fetchSchemaTranslations(); | ||
const full = await fetchJson<OsmResponse>(getOsmUrlOrFull(apiId)); | ||
const itemsMap = getItemsMap(full.elements); | ||
const feature = addSchemaToFeature( | ||
osmToFeature(itemsMap[apiId.type][apiId.id]), | ||
); | ||
return { | ||
...feature, | ||
memberFeatures: getMemberFeatures(feature.members, itemsMap), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { OsmElement } from './types'; | ||
import { Feature } from '../types'; | ||
import { addSchemaToFeature } from '../tagging/idTaggingScheme'; | ||
import { osmToFeature } from './osmToFeature'; | ||
|
||
export const getItemsMap = (elements: OsmElement[]) => { | ||
const itemsMap = { | ||
node: {} as Record<number, OsmElement<'node'>>, | ||
way: {} as Record<number, OsmElement<'way'>>, | ||
relation: {} as Record<number, OsmElement<'relation'>>, | ||
}; | ||
elements.forEach((element) => { | ||
itemsMap[element.type][element.id] = element; | ||
}); | ||
return itemsMap; | ||
}; | ||
|
||
export const getMemberFeatures = ( | ||
members: Feature['members'], | ||
itemsMap: ReturnType<typeof getItemsMap>, | ||
) => | ||
(members ?? []) | ||
.map(({ type, ref, role }) => { | ||
const element = itemsMap[type][ref]; | ||
if (!element) { | ||
return null; | ||
} | ||
|
||
const feature = addSchemaToFeature(osmToFeature(element)); | ||
feature.osmMeta.role = role; | ||
|
||
// TODO this code is not used, but it had some meaning, leaving for now :) | ||
// feature.center = element.center | ||
// ? [element.center.lon, element.center.lat] // from overpass "out center" | ||
// : feature.center; | ||
|
||
return feature; | ||
}) | ||
.filter(Boolean); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Position, SuccessInfo } from '../types'; | ||
import { fetchJson } from '../fetch'; | ||
import { API_SERVER, OSM_WEBSITE } from './consts'; | ||
|
||
export const insertOsmNote = async ( | ||
point: Position, | ||
text: string, | ||
): Promise<SuccessInfo> => { | ||
const [lon, lat] = point; | ||
|
||
const body = new URLSearchParams(); | ||
body.append('lat', `${lat}`); | ||
body.append('lon', `${lon}`); | ||
body.append('text', text); | ||
|
||
// {"type":"Feature","geometry":{"type":"Point","coordinates":[14.3244982,50.0927863]},"properties":{"id":26569,"url":"https://master.apis.dev.openstreetmap.org/api/0.6/notes/26569.json","comment_url":"https://master.apis.dev.openstreetmap.org/api/0.6/notes/26569/comment.json","close_url":"https://master.apis.dev.openstreetmap.org/api/0.6/notes/26569/close.json","date_created":"2021-04-17 10:37:44 UTC","status":"open","comments":[{"date":"2021-04-17 10:37:44 UTC","action":"opened","text":"way/39695868! Place was marked permanently closed.From https://osmapp.org/way/39695868","html":"\u003cp\u003eway/39695868! Place was marked permanently closed.From \u003ca href=\"https://osmapp.org/way/39695868\" rel=\"nofollow noopener noreferrer\"\u003ehttps://osmapp.org/way/39695868\u003c/a\u003e\u003c/p\u003e"}]}} | ||
const reply = await fetchJson(`${API_SERVER}/api/0.6/notes.json`, { | ||
nocache: true, | ||
method: 'POST', | ||
body, | ||
}); | ||
|
||
const noteId = reply.properties.id; | ||
return { | ||
type: 'note', | ||
text, | ||
url: `${OSM_WEBSITE}/note/${noteId}`, | ||
}; | ||
}; |
Oops, something went wrong.