Skip to content

Commit

Permalink
add shp support
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbllmnn committed Mar 20, 2018
1 parent c0496ad commit 4ec829a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"react-dom": "16.2.0",
"react-fa": "5.0.0",
"react-rnd": "7.3.1",
"shpjs": "^3.4.2",
"url-parse": "1.2.0",
"validator": "9.4.1"
},
Expand Down
51 changes: 37 additions & 14 deletions src/HigherOrderComponent/DropTargetMap/DropTargetMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import OlFormatGeoJSON from 'ol/format/geojson';
import OlLayerVector from 'ol/layer/vector';
import OlSourceVector from 'ol/source/vector';
import shp from 'shpjs';

/**
* HOC that adds layers to the map if GeoJSON files are dropped on it.
Expand All @@ -21,25 +22,47 @@ export function onDropAware(WrappedComponent) {
map: PropTypes.object
}

addGeojsonLayer = json => {
const format = new OlFormatGeoJSON();
const features = format.readFeatures(json);
const layer = new OlLayerVector({
source: new OlSourceVector({
features: features
})
});

this.props.map.addLayer(layer);
}

readGeojsonFile = file => {
const reader = new FileReader();
reader.readAsText(file);
reader.addEventListener('loadend', () => {
const content = reader.result;
this.addGeojsonLayer(content);
});
}

readShpFile = file => {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.addEventListener('loadend', () => {
const blob = reader.result;
shp(blob).then(this.addGeojsonLayer);
});
}

onDrop = event => {
event.preventDefault();
const files = event.dataTransfer.files;
const format = new OlFormatGeoJSON();
if (files.length > 0) {
for (let i = 0; i < files.length; ++i) {
const reader = new FileReader();
reader.readAsText(files[i]);
reader.addEventListener('loadend', () => {
const content = reader.result;
const features = format.readFeatures(content);
const layer = new OlLayerVector({
source: new OlSourceVector({
features: features
})
});

this.props.map.addLayer(layer);
});
const file = files[i];
if (file.name.match(/\.zip$/g)) {
this.readShpFile(file);
} else {
this.readGeojsonFile(file);
}
}
}
}
Expand Down

0 comments on commit 4ec829a

Please sign in to comment.