package com.luo.meteoinfo;
import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.meteoinfo.data.mapdata.MapDataManage;
import org.meteoinfo.global.PointD;
import org.meteoinfo.layer.VectorLayer;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.type.Name;
/**
* 通过meteoInfo 获取最大、最小经纬度 中心点坐标
*
* 通过geotools 获取边界线数据以及区县名 、区域编码、边界线数据
*
* @author 87830
*
*/
public class ShpFileData {
public static void main(String[] args) {
String path = "F:\\\\2020工作文档\\\\山西\\\\区县边界\\\\大同市_广灵县_行政边界\\\\大同市_广灵县_行政边界.shp";
Map<String, Object> shpFileDataByMeteoInfo = getShpFileDataByMeteoInfo(path);
Map<String, Object> shpFileDataByGeoTools = getShpFileDataByGeoTools(path);
// 将两个Map合并 最后将结果插入数据库中
shpFileDataByGeoTools.putAll(shpFileDataByMeteoInfo);
System.out.println(shpFileDataByGeoTools);
}
@SuppressWarnings("unused")
public static Map<String, Object> getShpFileDataByMeteoInfo(String path) {
Map<String, Object> meteoInfoMap = new HashMap<String, Object>();
VectorLayer shapeLayer = null;
try {
shapeLayer = MapDataManage.readMapFile_ShapeFile(path);
double _minX = shapeLayer.getExtent().minX;// 最小经度
double _maxX = shapeLayer.getExtent().maxX;// 最大经度
double _minY = shapeLayer.getExtent().minY;// 最小纬度
double _maxY = shapeLayer.getExtent().maxY;// 最大纬度
// System.out.println(_minX+"----_minX-----_maxX-------"+_maxX);
// System.out.println(_minY+"----_minY-----_maxY-------"+_maxY);
PointD centerPoint = shapeLayer.getExtent().getCenterPoint();// 获取到中心点坐标
double x = centerPoint.X;
double y = centerPoint.Y;
// System.out.println(x+"-----x------y------"+y);
meteoInfoMap.put("lon", x);
meteoInfoMap.put("lat", y);
meteoInfoMap.put("llon", _minX);
meteoInfoMap.put("rlon", _maxX);
meteoInfoMap.put("llat", _minY);
meteoInfoMap.put("rlat", _maxY);
} catch (Exception e) {
e.printStackTrace();
}
return meteoInfoMap;
}
@SuppressWarnings("unlikely-arg-type")
public static Map<String, Object> getShpFileDataByGeoTools(String path) {
Map<String, Object> geotoolsMap = new HashMap<String, Object>();
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
try {
ShapefileDataStore sds = (ShapefileDataStore) dataStoreFactory
.createDataStore(new File(path).toURI().toURL());
sds.setCharset(Charset.forName("GBK"));
SimpleFeatureSource featureSource = sds.getFeatureSource();
SimpleFeatureIterator itertor = featureSource.getFeatures().features();
while (itertor.hasNext()) {
SimpleFeature feature = itertor.next();
Iterator<Property> it = feature.getProperties().iterator();
List<Map<String, Object>> oldList = new ArrayList<Map<String, Object>>();
while (it.hasNext()) {
Property pro = it.next();
Object value = pro.getValue();
Name name = pro.getName();
if (value.toString().contains("MULTIPOLYGON")) {
String _value = value.toString().replace(")", "").replace("(", "").replace("MULTIPOLYGON", "");
String[] tmp = _value.split(",");
if (tmp.length > 2) {
Map<String, Object> flag = new HashMap<>();
// flag.put("flag", ";");
// oldList.add(flag);
for (int i = 0; i < tmp.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
String[] strTmp = tmp[i].split(" ");
if (strTmp.length == 3) {
map.put("longitude", strTmp[1].trim());
map.put("latitude", strTmp[2].trim());
}
oldList.add(map);
}
}
geotoolsMap.put("region", oldList);
}
geotoolsMap.put(name.toString(), value);
}
}
itertor.close();
} catch (Exception e) {
e.printStackTrace();
}
geotoolsMap.remove("the_geom");
return geotoolsMap;
}
}
oneW
- 粉丝: 0
- 资源: 2