Skip to content

Commit 7cf2425

Browse files
authored
Add back ShapeFile Support (#141)
* Add back ShapeFile Support * Lint
1 parent 39c696b commit 7cf2425

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

packages/base/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"devDependencies": {
8080
"@apidevtools/json-schema-ref-parser": "^9.0.9",
8181
"@types/node": "^18.15.11",
82+
"@types/shpjs": "^3.4.7",
8283
"@types/three": "^0.135.0",
8384
"@types/uuid": "^10.0.0",
8485
"rimraf": "^3.0.2",

packages/base/src/mainview/mainView.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
IVectorLayer,
2020
IVectorTileLayer,
2121
IVectorTileSource,
22+
IShapefileSource,
2223
IWebGlLayer,
2324
JupyterGISModel
2425
} from '@jupytergis/schema';
@@ -55,6 +56,7 @@ import * as React from 'react';
5556
import { isLightTheme } from '../tools';
5657
import { MainViewModel } from './mainviewmodel';
5758
import { Spinner } from './spinner';
59+
import shp from 'shpjs';
5860

5961
interface IProps {
6062
viewModel: MainViewModel;
@@ -221,6 +223,21 @@ export class MainView extends React.Component<IProps, IStates> {
221223
}
222224
}
223225

226+
private async _loadShapefileAsGeoJSON(
227+
url: string
228+
): Promise<GeoJSON.FeatureCollection | GeoJSON.FeatureCollection[]> {
229+
try {
230+
const response = await fetch(`/jupytergis_core/proxy?url=${url}`);
231+
const arrayBuffer = await response.arrayBuffer();
232+
const geojson = await shp(arrayBuffer);
233+
234+
return geojson;
235+
} catch (error) {
236+
console.error('Error loading shapefile:', error);
237+
throw error;
238+
}
239+
}
240+
224241
/**
225242
* Add a source in the map.
226243
*
@@ -305,6 +322,22 @@ export class MainView extends React.Component<IProps, IStates> {
305322

306323
break;
307324
}
325+
case 'ShapefileSource': {
326+
const parameters = source.parameters as IShapefileSource;
327+
328+
const geojson = await this._loadShapefileAsGeoJSON(parameters.path);
329+
const geojsonData = Array.isArray(geojson) ? geojson[0] : geojson;
330+
331+
const format = new GeoJSON();
332+
333+
newSource = new VectorSource({
334+
features: format.readFeatures(geojsonData, {
335+
dataProjection: 'EPSG:4326',
336+
featureProjection: this._Map.getView().getProjection()
337+
})
338+
});
339+
break;
340+
}
308341
case 'ImageSource': {
309342
const sourceParameters = source.parameters as IImageSource;
310343

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ __metadata:
898898
"@rjsf/core": ^4.2.0
899899
"@types/d3-color": ^3.1.0
900900
"@types/node": ^18.15.11
901+
"@types/shpjs": ^3.4.7
901902
"@types/three": ^0.135.0
902903
"@types/uuid": ^10.0.0
903904
ajv: ^8.14.0
@@ -3255,6 +3256,16 @@ __metadata:
32553256
languageName: node
32563257
linkType: hard
32573258

3259+
"@types/shpjs@npm:^3.4.7":
3260+
version: 3.4.7
3261+
resolution: "@types/shpjs@npm:3.4.7"
3262+
dependencies:
3263+
"@types/geojson": "*"
3264+
"@types/node": "*"
3265+
checksum: da6883f802e50bddfc00ae6446bee3947e76d56b01b8e633fd4be565b4021365dca35a3f8949f64f7e1e86c076a3afaeae75ee8e0563115425b56dd8137c0f40
3266+
languageName: node
3267+
linkType: hard
3268+
32583269
"@types/source-list-map@npm:*":
32593270
version: 0.1.6
32603271
resolution: "@types/source-list-map@npm:0.1.6"

0 commit comments

Comments
 (0)