feat(ui-osm): adjust to Overpass API

This commit is contained in:
dancingCycle 2023-09-13 11:43:38 +02:00
parent fa2e63a199
commit 2d10ca86e6
11 changed files with 230 additions and 373 deletions

View File

@ -1,14 +1,16 @@
import React from 'react';
import { BrowserRouter as Router, Link, Route, Routes } from 'react-router-dom';
import Entity from './pages/entity';
import EntityOsm from './pages/entity-osm';
import Header from './components/header';
import Home from './pages/home';
import packageInfo from '../package.json'
import config from './utils/config';
const VERSION = packageInfo.version;
const OVERPASS_API = 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:60]';
const area = '(area);out body center qt;';
export default function App(){
return (
@ -21,13 +23,29 @@ export default function App(){
<Routes>
<Route path='/' element={<Home/>}/>
<Route path='/bike-ride' element={<EntityOsm
address={OVERPASS_API + ';relation(4189512);map_to_area;nwr[amenity=bicycle_parking][bike_ride]["bike_ride"!="no"](area);out body center qt;'}
address={config.ADDRESS + 'nwr[amenity=bicycle_parking][bike_ride]["bike_ride"!="no"]' + config.AREA }
title='B+R Stations'/>}/>
<Route path='/park-ride' element={<Entity apiRoute='park-ride/info' title='P+R Stations'/>}/>
<Route path='/taxi' element={<Entity apiRoute='taxi/info' title='Taxi Stations'/>}/>
<Route path='/ticket-machine' element={<Entity apiRoute='ticket-machine/info' title='Ticket Machines'/>}/>
<Route path='/ticket-office' element={<Entity apiRoute='ticket-office/info' title='Ticket Offices'/>}/>
<Route path='/train-station' element={<Entity apiRoute='train-station/info' title='Train Stations'/>}/>
<Route path='/bus-stop' element={<EntityOsm
address={ config.ADDRESS + 'nwr[highway=bus_stop]' + config.AREA }
title='Bus Stops'/>}/>
<Route path='/park-ride' element={<EntityOsm
address={ config.ADDRESS + 'nwr["park_ride"!="no"]["park_ride"]' + config.AREA }
title='P+R Stations'/>}/>
<Route path='/taxi' element={<EntityOsm
address={ config.ADDRESS + 'nwr[amenity=taxi]' + config.AREA }
title='Taxi Stations'/>}/>
<Route path='/ticket-machine' element={<EntityOsm
address={ config.ADDRESS + 'nwr[amenity=vending_machine][vending=public_transport_tickets]' + config.AREA }
title='Ticket Machines'/>}/>
<Route path='/ticket-office' element={<EntityOsm
address={ config.ADDRESS + 'nwr[shop=ticket]["tickets:public_transport"!=no]' + config.AREA }
title='Ticket Offices'/>}/>
<Route path='/train-station' element={<EntityOsm
address={ config.ADDRESS + 'nwr[railway=station][usage!=tourism]' + config.AREA }
title='Train Stations'/>}/>
<Route path='/train-halt' element={<EntityOsm
address={ config.ADDRESS + 'nwr[railway=halt][usage!=tourism]' + config.AREA }
title='Train Halts'/>}/>
</Routes>
</Router>
);

View File

@ -20,14 +20,15 @@ export default function FetchOsm ({address, title}) {
/*TODO handle errors: https://www.valentinog.com/blog/await-react/*/
//console.log("Fetch:fetch(): address: " + address);
const res = await get(address);
console.log("Fetch:fetch(): res.length: "+res.elements.length);
//console.log("Fetch:fetch(): res.length: "+res.elements.length);
setArray((array) => res.elements);
} catch (err) {
console.error('err.message: ' + err.message);
setArray((array) => []);
}
}else{
console.error('config or address NOT available');
//console.log("Fetch:fetch(): address: " + address);
console.error('Fetch:fetch(): config or address NOT available');
}
};
@ -39,7 +40,7 @@ export default function FetchOsm ({address, title}) {
}, []);
if (array !== undefined || array !== null || array.length > 0) {
if ( array !== undefined && array !== null && array.length > 0 ) {
/*return a React element*/
return (
<>

View File

@ -1,67 +0,0 @@
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
import config from '../utils/config';
import Map from './map';
import Table from './table';
/*destructure props object*/
export default function Fetch ({route, title}) {
const [array, setArray] = useState([]);
/*fetch array in a JavaScript function*/
const fetch = async () => {
if(config && route){
try {
//console.log("Fetch:fetch(): route: " + route);
/*TODO handle errors: https://www.valentinog.com/blog/await-react/*/
const address = `${config.API}${route}`;
//console.log("Fetch:fetch(): address: "+address);
const res = await axios.get(address);
//console.log("Fetch:fetch(): res.length: "+res.data.length);
setArray((array) => res.data);
} catch (err) {
console.error('err.message: ' + err.message);
setArray((array) => []);
}
}else{
console.error('config or route NOT available');
}
};
useEffect(() => {
/*effect goes here*/
fetch();
/*use an empty dependency array to ensure the hook is running only once*/
/*TODO study dependency array: https://reactjs.org/docs/hooks-effect.html*/
}, []);
if (array !== undefined || array !== null || array.length > 0) {
/*return a React element*/
return (
<>
<Map
array={array}
title={title}
/>
<Table
array={array}
title={title}
/>
</>
);
}else{
return (
<>
<p>Loading...</p>
</>
);
}
};
Fetch.propTypes = {
route: PropTypes.string,
title: PropTypes.string
};

View File

@ -5,18 +5,18 @@ import EntitiesMap from './map/entities-map';
/*destructure props object*/
export default function Map ({array, title}){
if (array !== undefined || array !== null || array.length > 0) {
if ( array !== undefined && array !== null && array.length > 0 ) {
/*return a React element*/
return (
<>
<h1>Map of {title}</h1>
<p>Map of {array.length}&nbsp;{title}</p>
<EntitiesMap entities={array} />
</>
);
}else{
return (
<>
<p>Map loading...</p>
<p>Loading...</p>
</>
);
}

View File

@ -13,6 +13,15 @@ export default function EntitiesMap({entities}) {
//const position = [52.26594, 10.52673];
/*center map in the middle of RVB*/
const position = [52.16594, 10.52673];
if ( entities !== undefined && entities !== null && entities.length > 0 ) {
/*return a React element*/
}else{
return (
<>
<p>Loading...</p>
</>
);
}
return (
<>
<MapContainer
@ -30,8 +39,8 @@ export default function EntitiesMap({entities}) {
/>
{
entities.map(function(value,key) {
if( value.type === "node"){
entities.map( function( value, key ) {
if ( value.type === "node"){
return <EntitiesMarker
key={value.id}
index={value.id}
@ -40,7 +49,7 @@ export default function EntitiesMap({entities}) {
lon={value.lon}
name={value.tags.name}
/>;
} else if( value.type === "way" ) {
} else if ( value.type === "way" ) {
return <EntitiesMarker
key={value.id}
index={value.id}
@ -50,7 +59,7 @@ export default function EntitiesMap({entities}) {
name={value.tags.name}
/>;
} else {
console.error("entities-map: OSM type NOT known");
console.error("entities-map: OSM type (" + value.type + ") NOT known");
}
})
}

View File

@ -2,7 +2,7 @@ import React from 'react';
import PropTypes from 'prop-types';
function TableEntries ({array}) {
if (array !== undefined || array !== null || array.length > 0) {
if ( array !== undefined && array !== null && array.length > 0 ) {
//iterate over array
return array.map((item, index) => {
if( item.type === "node"){

View File

@ -5,11 +5,11 @@ import TableEntries from './table-entries';
/*destructure props object*/
export default function Table ({array, title}){
if (array !== undefined || array !== null || array.length > 0) {
if ( array !== undefined && array !== null && array.length > 0 ) {
/*return a React element*/
return (
<>
<h1>Table of {title}</h1>
<p>Table of {array.length}&nbsp;{title}</p>
<table>
<thead>
<tr>

View File

@ -1,19 +0,0 @@
import React from 'react'
import PropTypes from 'prop-types';
import Fetch from '../components/fetch';
export default function Entity({apiRoute, title}) {
return(
<>
<Fetch
route={apiRoute}
title={title}
/>
</>
);
};
Entity.propTypes = {
apiRoute: PropTypes.string,
title: PropTypes.string
};

View File

@ -3,151 +3,204 @@ import { Link} from 'react-router-dom';
import { get } from '../utils/request';
import {getBusStopCount, getBikeRideCount, getParkRideCount, getTrainStationCount, getTaxiCount, getTicketMachineCount, getTicketOfficeCount} from '../utils/api';
export default function Home(){
const [sttnRry, setSttnRry] = useState([]);
const [bikeRideCount, setBikeRideCount] = useState('loading...');
const [busStopCount, setBusStopCount] = useState('loading...');
const [parkRideCount, setParkRideCount] = useState('loading...');
const [parkRideRry, setParkRideRry] = useState('loading...');
const [taxiCount, setTaxiCount] = useState('loading...');
const [ticketMachineCount, setTicketMachineCount] = useState('loading...');
const [ticketOfficeCount, setTicketOfficeCount] = useState('loading...');
const [trainStationCount, setTrainStationCount] = useState('loading...');
const fetchBikeRideCount = async () => {
try {
const rsp=await getBikeRideCount();
setBikeRideCount((bikeRideCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setBikeRideCount((bikeRideCount) => 'loading...');
}
};
const fetchBusStopCount = async () => {
try {
const rsp=await getBusStopCount();
setBusStopCount((busStopCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setBusStopCount((busStopCount) => 'loading...');
}
};
const fetchParkRideCount = async () => {
try {
const rsp=await getParkRideCount();
setParkRideCount((parkRideCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setParkRideCount((parkRideCount) => 'loading...');
}
};
const fetchTaxiCount = async () => {
try {
const rsp=await getTaxiCount();
setTaxiCount((taxiCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setTaxiCount((taxiCount) => 'loading...');
}
};
const fetchTicketMachineCount = async () => {
try {
const rsp=await getTicketMachineCount();
setTicketMachineCount((ticketMachineCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setTicketMachineCount((ticketMachineCount) => 'loading...');
}
};
const fetchTicketOfficeCount = async () => {
try {
const rsp=await getTicketOfficeCount();
setTicketOfficeCount((ticketOfficeCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setTicketOfficeCount((ticketOfficeCount) => 'loading...');
}
};
const fetchTrainStationCount = async () => {
try {
const rsp=await getTrainStationCount();
setTrainStationCount((trainStationCount) => rsp);
} catch (err) {
console.error('err.message: ' + err.message);
setTrainStationCount((trainStationCount) => 'loading...');
}
};
const [trainHaltCount, setTrainHaltCount] = useState('loading...');
useEffect(() => {
/*effect goes here*/
const address = 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:60];relation(4189512);map_to_area;nwr[amenity=bicycle_parking][bike_ride]["bike_ride"!="no"](area);out body center qt;';
const address = 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:60];relation(4189512);map_to_area;';
const area = '(area);out body center qt;';
////console.log('StationProfile:useEffect() address: ' + address);/
//get station array
get(address)
//bike ride
get(address + 'nwr[amenity=bicycle_parking][bike_ride]["bike_ride"!="no"]' + area)
.then(data => {
setSttnRry((sttnRry) => data.elements);
setBikeRideCount(data.elements.length);
});
//fetch
fetchBusStopCount();
fetchBikeRideCount();
fetchParkRideCount();
fetchTicketMachineCount();
fetchTicketOfficeCount();
fetchTrainStationCount();
fetchTaxiCount();
//bus stop
get(address + 'nwr[highway=bus_stop]' + area)
.then(data => {
setBusStopCount(data.elements.length);
});
//park ride
get(address + 'nwr["park_ride"!="no"]["park_ride"]' + area)
.then(data => {
setParkRideRry(data.elements.length);
});
//taxi
get(address + 'nwr[amenity=taxi]' + area)
.then(data => {
setTaxiCount(data.elements.length);
});
//ticket machines
get(address + 'nwr[amenity=vending_machine][vending=public_transport_tickets]' + area)
.then(data => {
setTicketMachineCount(data.elements.length);
});
//ticket offices
get(address + 'nwr[shop=ticket]["tickets:public_transport"!=no]' + area)
.then(data => {
setTicketOfficeCount(data.elements.length);
});
//train stations
get(address + 'nwr[railway=station][usage!=tourism]' + area)
.then(data => {
setTrainStationCount(data.elements.length);
});
//train halts
get(address + 'nwr[railway=halt][usage!=tourism]' + area)
.then(data => {
setTrainHaltCount(data.elements.length);
});
}, []);
return (
<>
<p>Number of bike and ride (B&R) stations in the RVB area:&nbsp;{sttnRry.length}</p>
<p>Number of bike and ride (B&R) stations in the RVB area: {bikeRideCount}</p>
<Link
to={'/bike-ride'}
>
<button>Details for B&R Stations</button>
</Link>
<p>Number of bus stops in the RVB area: {busStopCount}</p>
<p>Number of park and ride (P&R) stations in the RVB area: {parkRideCount}</p>
<Link
to={'/park-ride'}
>
<button>Details for P&R Stations</button>
</Link>
<p>Number of taxi stations in the RVB area: {taxiCount}</p>
<Link
to={'/taxi'}
>
<button>Details for Taxi Stations</button>
</Link>
<p>Number of ticket machines in the RVB area: {ticketMachineCount}</p>
<Link
to={'/ticket-machine'}
>
<button>Details for Ticket Machines</button>
</Link>
<p>Number of ticket offices in the RVB area: {ticketOfficeCount}</p>
<Link
to={'/ticket-office'}
>
<button>Details for Ticket Offices</button>
</Link>
<p>Number of train stations in the RVB area: {trainStationCount}</p>
<Link
to={'/train-station'}
>
<button>Details for Train Stations</button>
</Link>
<table>
<thead>
<tr>
<th>count</th>
<th>feature</th>
</tr>
</thead>
<tbody>
<tr
key='br'
>
<td>
<Link
to={'/bike-ride'}
>
<button>
{bikeRideCount}
</button>
</Link>
</td>
<td>Bike and ride (B+R) stations
</td>
</tr>
<tr
key='bs'
>
<td>
<Link
to={'/bus-stop'}
>
<button>
{busStopCount}
</button>
</Link>
</td>
<td>Bus stops
</td>
</tr>
<tr
key='pr'
>
<td>
<Link
to={'/park-ride'}
>
<button>
{parkRideRry}
</button>
</Link>
</td>
<td>Park and ride (P+R) stations
</td>
</tr>
<tr
key='taxi'
>
<td>
<Link
to={'/taxi'}
>
<button>
{taxiCount}
</button>
</Link>
</td>
<td>Taxi stations
</td>
</tr>
<tr
key='ticket-machine'
>
<td>
<Link
to={'/ticket-machine'}
>
<button>
{ticketMachineCount}
</button>
</Link>
</td>
<td>Ticket machines
</td>
</tr>
<tr
key='ticket-office'
>
<td>
<Link
to={'/ticket-office'}
>
<button>
{ticketOfficeCount}
</button>
</Link>
</td>
<td>Ticket offices
</td>
</tr>
<tr
key='train-station'
>
<td>
<Link
to={'/train-station'}
>
<button>
{trainStationCount}
</button>
</Link>
</td>
<td>Train Stations
</td>
</tr>
<tr
key='train-halt'
>
<td>
<Link
to={'/train-halt'}
>
<button>
{trainHaltCount}
</button>
</Link>
</td>
<td>Train Halts
</td>
</tr>
</tbody>
</table>
</>
);
}

View File

@ -1,139 +0,0 @@
import axios from 'axios';
import config from './config';
/*
*get count of bus stops
*/
export async function getBusStopCount(){
//console.log('getBusStopCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}bus-stop/count`;
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getBusStopCount() Done.');
return count;
};
/*
*get count of park and ride (P&R)
*/
export async function getParkRideCount(){
//console.log('getParkRideCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}park-ride/count`;
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getParkRideCount() Done.');
return count;
};
/*
*get count of bike and ride (B&R)
*/
export async function getBikeRideCount(){
//console.log('getBikeRideCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}bike-ride/count`;
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getBikeRideCount() Done.');
return count;
};
/*
*get count of train stations
*/
export async function getTrainStationCount(){
//console.log('getTrainStationCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}train-station/count`;
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getTrainStationCount() Done.');
return count;
};
/*
*get count of taxi stations
*/
export async function getTaxiCount(){
//console.log('getTaxiCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}taxi/count`;
//console.log('getTaxiCount() address: ' + address);
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getTaxiCount() Done.');
return count;
};
/*
*get count of ticket machines
*/
export async function getTicketMachineCount(){
//console.log('getTicketMachineCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}ticket-machine/count`;
//console.log('getTicketMachineCount() address: ' + address);
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getTicketMachineCount() Done.');
return count;
};
/*
*get count of ticket offices
*/
export async function getTicketOfficeCount(){
//console.log('getTicketOfficeCount() Start...');
let count=null;
try {
//TODO handle errors: https://www.valentinog.com/blog/await-react/
const address = `${config.API}ticket-office/count`;
//console.log('getTicketOfficeCount() address: ' + address);
const rsp = await axios.get(address);
count = rsp.data[0][0];
} catch (err) {
console.error('err.message: ' + err.message);
}
//console.log('getTicketOfficeCount() Done.');
return count;
};

View File

@ -1,4 +1,5 @@
export default {
API: 'https://v1rvb.api.swingbe.de/'
API: 'https://v1rvb.api.swingbe.de/',
ADDRESS: 'https://overpass-api.de/api/interpreter?data=[out:json][timeout:60];relation(4189512);map_to_area;',
AREA: '(area);out body center qt;',
};