colouring-montreal/app/src/frontend/map.js

159 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-09-13 15:35:27 -04:00
import React, { Component, Fragment } from 'react';
import { Map, TileLayer, ZoomControl, AttributionControl } from 'react-leaflet-universal';
import '../../node_modules/leaflet/dist/leaflet.css'
import './map.css'
2019-02-11 04:04:19 -05:00
2019-01-19 14:06:26 -05:00
import { HelpIcon } from './icons';
2019-02-11 04:04:19 -05:00
import Legend from './legend';
import { parseCategoryURL } from '../parse';
import SearchBox from './search-box';
import ThemeSwitcher from './theme-switcher';
const OS_API_KEY = 'NVUxtY5r8eA6eIfwrPTAGKrAAsoeI9E9';
/**
* Map area
*/
class ColouringMap extends Component {
2018-09-10 07:40:25 -04:00
constructor(props) {
super(props);
this.state = {
2018-09-27 16:37:32 -04:00
theme: 'night',
2018-09-10 07:40:25 -04:00
lat: 51.5245255,
lng: -0.1338422,
2018-09-10 18:34:56 -04:00
zoom: 16
2018-09-10 07:40:25 -04:00
};
this.handleClick = this.handleClick.bind(this);
2019-02-11 04:04:19 -05:00
this.handleLocate = this.handleLocate.bind(this);
2018-09-13 11:03:49 -04:00
this.themeSwitch = this.themeSwitch.bind(this);
2018-09-10 07:40:25 -04:00
}
2019-02-11 04:04:19 -05:00
handleLocate(lat, lng, zoom){
this.setState({
lat: lat,
lng: lng,
zoom: zoom
})
}
2018-09-10 07:40:25 -04:00
handleClick(e) {
2018-11-29 17:00:53 -05:00
const is_edit = this.props.match.url.match('edit')
const mode = is_edit? 'edit': 'view';
const lat = e.latlng.lat
const lng = e.latlng.lng
2018-11-30 04:26:28 -05:00
const new_cat = parseCategoryURL(this.props.match.url);
2018-11-29 17:00:53 -05:00
const map_cat = new_cat || 'age';
2018-09-10 07:40:25 -04:00
fetch(
2018-09-30 16:10:03 -04:00
'/buildings/locate?lat='+lat+'&lng='+lng
2018-09-10 07:40:25 -04:00
).then(
(res) => res.json()
).then(function(data){
2018-09-30 16:54:47 -04:00
if (data && data.length){
const building = data[0];
this.props.selectBuilding(building);
2018-11-29 17:00:53 -05:00
this.props.history.push(`/${mode}/${map_cat}/building/${building.building_id}.html`);
2018-09-10 07:40:25 -04:00
} else {
// deselect but keep/return to expected colour theme
this.props.selectBuilding(undefined);
2018-11-29 17:00:53 -05:00
this.props.history.push(`/${mode}/${map_cat}.html`);
2018-09-10 07:40:25 -04:00
}
2018-09-10 18:34:56 -04:00
}.bind(this)).catch(
(err) => console.error(err)
)
}
2018-09-13 11:03:49 -04:00
themeSwitch(e) {
e.preventDefault();
const newTheme = (this.state.theme === 'light')? 'night' : 'light';
this.setState({theme: newTheme});
}
render() {
const position = [this.state.lat, this.state.lng];
2018-09-13 18:55:53 -04:00
// baselayer
const key = OS_API_KEY
const tilematrixSet = 'EPSG:3857'
2018-09-13 11:03:49 -04:00
const layer = (this.state.theme === 'light')? 'Light 3857' : 'Night 3857';
const url = `https://api2.ordnancesurvey.co.uk/mapping_api/v1/service/zxy/${tilematrixSet}/${layer}/{z}/{x}/{y}.png?key=${key}`
const attribution = 'Building attribute data is © Colouring London contributors. Maps contain OS data © Crown copyright: OS Maps baselayers and building outlines.'
2018-09-10 17:14:09 -04:00
2018-09-13 18:55:53 -04:00
// colour-data tiles
const is_building = /building/.test(this.props.match.url);
2019-01-19 14:06:26 -05:00
const is_edit = /edit/.test(this.props.match.url);
2018-11-30 04:26:28 -05:00
const cat = parseCategoryURL(this.props.match.url);
const tileset_by_cat = {
age: 'date_year',
size: 'size_storeys',
location: 'location',
like: 'likes',
planning: 'conservation_area',
}
const data_tileset = tileset_by_cat[cat];
// pick revision id to bust browser cache
const rev = this.props.building? this.props.building.revision_id : '';
const dataLayer = data_tileset?
2018-12-05 15:39:16 -05:00
<TileLayer
key={data_tileset}
url={`/tiles/${data_tileset}/{z}/{x}/{y}.png?rev=${rev}`}
2019-02-24 10:29:39 -05:00
minZoom={9} />
: null;
2018-09-10 18:34:56 -04:00
2018-09-13 18:55:53 -04:00
// highlight
2018-09-10 18:34:56 -04:00
const geometry_id = (this.props.building) ? this.props.building.geometry_id : undefined;
const highlight = `/tiles/highlight/{z}/{x}/{y}.png?highlight=${geometry_id}`
const highlightLayer = (is_building && this.props.building) ?
2018-12-05 15:39:16 -05:00
<TileLayer
key={this.props.building.building_id}
url={highlight}
minZoom={14} />
: null;
2018-09-10 17:14:09 -04:00
const base_layer_url = (this.state.theme === 'light')?
`/tiles/base_light/{z}/{x}/{y}.png`
: `/tiles/base_night/{z}/{x}/{y}.png`
return (
2018-09-13 15:35:27 -04:00
<Fragment>
<Map
center={position}
zoom={this.state.zoom}
2019-02-24 10:29:39 -05:00
minZoom={9}
2018-09-13 15:35:27 -04:00
maxZoom={18}
doubleClickZoom={false}
zoomControl={false}
attributionControl={false}
onClick={this.handleClick}
>
<TileLayer url={url} attribution={attribution} />
2018-12-05 15:39:16 -05:00
<TileLayer url={base_layer_url} minZoom={14} />
2018-09-13 18:55:53 -04:00
{ dataLayer }
2018-09-13 15:35:27 -04:00
{ highlightLayer }
<ZoomControl position="topright" />
<AttributionControl prefix="" />
</Map>
2019-01-19 14:06:26 -05:00
{
2019-01-19 14:19:00 -05:00
!is_building && this.props.match.url !== '/'? (
2019-01-22 14:39:16 -05:00
<div className="map-notice">
2019-01-19 14:06:26 -05:00
<HelpIcon /> {is_edit? 'Click a building to edit' : 'Click a building for details'}
</div>
) : null
}
2019-02-11 04:04:19 -05:00
{
this.props.match.url !== '/'? (
2019-04-18 12:41:20 -04:00
<Fragment>
<Legend slug={cat} />
<ThemeSwitcher onSubmit={this.themeSwitch} currentTheme={this.state.theme} />
2019-02-11 04:04:19 -05:00
<SearchBox onLocate={this.handleLocate} is_building={is_building} />
2019-04-18 12:41:20 -04:00
</Fragment>
2019-02-11 04:04:19 -05:00
) : null
}
2018-09-13 15:35:27 -04:00
</Fragment>
);
}
};
export default ColouringMap;