uniapp通过绝对路径解压zpi中的shpe转化为geojson
async fileResult() {
const filepath11 = '/storage/emulated/0/importData/Export_Output_6.zip';
// Base64解码函数
function base64ToArrayBuffer(base64) {
const binaryString = atob(base64.split(',')[1]);
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes.buffer;
}
function readFileAsBase64(filepath, callback) {
// 使用 plus.io.resolveLocalFileSystemURL 获取文件对象
plus.io.resolveLocalFileSystemURL(filepath, (entry) => {
// 使用 entry.file 获取文件对象
entry.file((file) => {
const reader = new plus.io.FileReader();
// 使用 readAsDataURL 方法读取文件内容
reader.readAsDataURL(file);
// 定义 onloadend 回调
reader.onloadend = (e) => {
const base64 = e.target.result;
callback(base64);
};
// 定义 onerror 回调
reader.onerror = (e) => {
console.error('读取文件错误:', e);
};
}, (error) => {
console.error('获取文件失败:', error);
});
}, (error) => {
console.error('解析文件路径失败:', error);
});
}
// 读取文件并转换为 ArrayBuffer
readFileAsBase64(filepath11, (base64) => {
const arrayBuffer = base64ToArrayBuffer(base64);
JSZip.loadAsync(arrayBuffer).then((zip) => {
const shapefiles = zip.file(/.*\.shp$/i); // 正则匹配 .shp 文件
if (shapefiles.length) {
shapefiles[0].async('arraybuffer').then((shpArrayBuffer) => {
shapefile.read(shpArrayBuffer).then((geojson) => {
console.log(geojson, '转好的geojson数据');
// self.handleGeojson(geojson, data.file);
}).catch((error) => {
console.error('读取 shapefile 失败:', error);
});
}).catch((error) => {
console.error('读取 .shp 文件失败:', error);
});
} else {
console.error('未找到 .shp 文件');
}
}).catch((error) => {
console.error('解压缩 ZIP 文件失败:', error);
});
});
},
把geojson转化为wkt:
ShpeFileZip.js:
/**
* 检索组件绘制方法封装: DrawArea
*/
import Draw from 'ol/interaction/Draw.js';
import {
OSM,
Vector as VectorSource
} from 'ol/source.js';
import {
Tile as TileLayer,
Vector as VectorLayer
} from 'ol/layer.js';
import {
Circle,
Fill,
Stroke,
Style,
} from 'ol/style';
import {
GeometryCollection,
LineString,
LinearRing,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
} from 'ol/geom.js';
import {
createRegularPolygon
} from 'ol/interaction/Draw.js';
import {
WKT
} from 'ol/format.js';
import {
fromCircle
} from 'ol/geom/Polygon';
import * as jsts from 'jsts/dist/jsts.min.js';
export class ShpeFileZip {
format = new WKT();
jstsPraser;
getJstsPraser() {
return this.jstsPraser;
}
/**
* 初始化jsts的parser
*/
innitParser() {
const parser = new jsts.io.OL3Parser();
parser.inject(
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
);
this.jstsPraser = parser;
}
constructor() {
this.innitParser();
}
/**
* 把中文件中读取到的geojson转化为wkt或者geometry
*
* @params geojson:范围数据
* @params type: wkt,geom
* return: wkt/geometry
*/
hansleZipGeojson(geojson, type = 'wkt') {
const format = this.format;
const parser = this.jstsPraser;
return new Promise((resolve) => {
// 工作区范围features
const workspaceFeatures = geojson.features;
try {
// 如果工作区范围是多个feature,合并成一个
let newWfGeometry;
if (workspaceFeatures.length > 0) {
workspaceFeatures.forEach((wkFeature) => {
// 获取类型
const {
type,
coordinates: mapdata
} = wkFeature.geometry;
let geometry;
if (type === 'Polygon') {
geometry = new Polygon(mapdata); // 创建一个 Polygon 对象
const wfGeometry1 = parser.read(geometry);
newWfGeometry = newWfGeometry ? newWfGeometry.union(wfGeometry1) :
wfGeometry1;
} else if (type === 'MultiPolygon') {
// 为每个多边形创建 Polygon
const polygons = mapdata.map((polygonCoords) => new Polygon(
polygonCoords));
geometry = new MultiPolygon(polygons); // 创建一个 MultiPolygon 对象
const wfGeometry1 = parser.read(geometry);
newWfGeometry = newWfGeometry ? newWfGeometry.union(wfGeometry1) :
wfGeometry1;
} else {
uni.showToast({
icon: 'none',
title: '区域范围必须是面数据!',
position: 'bottom', // (仅App生效)
});
resolve(null);
}
});
if (newWfGeometry) {
// 转为wkt
const newWkt = format.writeGeometry(parser.write(newWfGeometry));
resolve(newWkt);
}
} else {
this.$message.error('数据有误,请重新上传!');
resolve(null);
}
} catch (e) {
console.log('解析失败!', e);
resolve(null);
}
});
}
}
使用:
import {
ShpeFileZip
} from '@/map/ShpeFileZip.js';
// 这里只放了关键代码----
data() {
return {
ShpeFileZip: null,
}
},
mounted() {
// this.getBetter1meterRanking();
this.ShpeFileZip = new ShpeFileZip();
},
// 调用
this.ShpeFileZip.hansleZipGeojson(geojson).then((wkt) => {
console.log('wkt数据', wkt);
});