File size: 4,401 Bytes
de234b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, {useState, useEffect} from 'react';
import {createRoot} from 'react-dom/client';
import {Map} from 'react-map-gl';
import maplibregl from 'maplibre-gl';
import {AmbientLight, PointLight, LightingEffect} from '@deck.gl/core';
import DeckGL from '@deck.gl/react';
import {PolygonLayer} from '@deck.gl/layers';
import {TripsLayer} from '@deck.gl/geo-layers';
import { Box } from '@mui/system';


// Source data CSV, Attribution to CARTO, OpenStreetMap contributors to deck.gl trips-layer example
const DATA_URL = {
//   BUILDINGS:'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/trips/buildings.json', // eslint-disable-line
    BUILDINGS: '/map1.json'
};

const ambientLight = new AmbientLight({
  color: [255, 255, 255],
  intensity: 6.0
});

const pointLight = new PointLight({
  color: [255, 255, 255],
  intensity: 1.0,
  position: [-6.4741164,
    53.4622087, 8000]
});

// const lightingEffect = new LightingEffect({ambientLight, pointLight});
const lightingEffect = new LightingEffect({ambientLight, pointLight});


const material = {
  ambient: 0.1,
  diffuse: 0.6,
  shininess: 32,
  specularColor: [60, 64, 70]
};

const DEFAULT_THEME = {
  buildingColor: [51, 204, 255],
  trailColor0: [253, 128, 93],
  trailColor1: [23, 184, 190],
  material,
  effects: [lightingEffect]
};

const INITIAL_VIEW_STATE = {
  longitude: -6.269218,
  latitude: 53.342505,
  zoom: 14.64,
  pitch: 59.31,
  bearing: 0
};

const MAP_STYLE = 'https://basemaps.cartocdn.com/gl/dark-matter-nolabels-gl-style/style.json';

const landCover = [
  [
    [-74.0, 40.7],
    [-74.02, 40.7],
    [-74.02, 40.72],
    [-74.0, 40.72]
  ]
];

export default function Map3D({
    buildings = DATA_URL.BUILDINGS,
    initialViewState = INITIAL_VIEW_STATE,
    mapStyle = MAP_STYLE,
    theme = DEFAULT_THEME,
  }) {
    const [buildingData, setBuildingData] = useState(null);
    const [viewState, setViewState] = useState(initialViewState); // Add this line to manage view state

    useEffect(() => {
        fetch(buildings)
          .then((response) => response.json())
          .then((json) => {
            const allData = json.datasets[0].data.allData.map(([feature]) => feature);
            setBuildingData(allData);
          })
          .catch((error) => {
            console.error('Error fetching buildings data:', error);
          });
      }, [buildings]);
    
    if (!buildingData) {
    return <div>Loading...</div>;
    }

    const layers = [
        new PolygonLayer({
            id: 'ground',
            data: landCover,
            getPolygon: f => f,
            stroked: false,
            getFillColor: [0, 0, 0, 0]
        }),
        new PolygonLayer({
            id: 'buildings',
            data: buildingData,
            extruded: true,
            wireframe: false,
            opacity: 0.5,
            getPolygon: (f) => f.geometry.coordinates[0][0],
            getElevation: (f) => f.properties.Height_Met,
            getFillColor: theme.buildingColor,
            material: theme.material,
          })
    ];

    const onViewStateChange = ({ viewState }) => {
        setViewState(viewState);
    };
  
    return (
        <Box sx={{ width: '100%', height: '100%', position: 'relative', borderRadius: '15px', overflow: 'hidden'}}>
            <DeckGL
                style={{ width: '100%', height: '100%', position: 'absolute'}}
                layers={layers}
                effects={theme.effects}
                initialViewState={initialViewState}
                controller={true}
                viewState={viewState}
                onViewStateChange={onViewStateChange}
            >
                <Map reuseMaps mapLib={maplibregl} mapStyle={mapStyle} preventStyleDiffing={true} attributionControl={false} />
            </DeckGL>
            <div
        style={{
          position: 'absolute',
          bottom: '10px',
          right: '10px',
          color: 'white',
          background: 'rgba(0, 0, 0, 0.5)',
          padding: '5px',
          borderRadius: '5px',
        }}
      >
        Longitude: {viewState.longitude.toFixed(6)}
        <br />
        Latitude: {viewState.latitude.toFixed(6)}
        <br />
        Zoom: {viewState.zoom.toFixed(2)}
        <br />
        Pitch: {viewState.pitch.toFixed(2)}
        <br />
        Bearing: {viewState.bearing.toFixed(2)}
      </div>
        </Box>
    );
  }