微信搜索:“二十同学” 公众号,欢迎关注一条不一样的成长之路
读写mid、mif文件操作工具类
装载mid文件,将mid文件里的数据变为 List<List<String>> 形式的
public List<List<String>> loadMidFile(File file) {
List<List<String>> midRows= new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
String line;
while ((line = reader.readLine()) != null) {
List<String> cols = Arrays.asList(line.split(",",-1));
if (cols.size() > 0) {
for (int i = 0; i < cols.size(); i++) {
String value = cols.get(i).replace("\"", "");
cols.set(i, value);
}
midRows.add(cols);
}
}
reader.close();
} catch (FileNotFoundException e) {
logger.error("loadMidFile():", e);
} catch (IOException e) {
logger.error("loadMidFile():", e);
}
return midRows;
}
装载mif文件
public List<String> loadMifFile(File file, Boolean isLoadMifData) {
//colField 为mid文件里各个值对应的字段
List<String> colField = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
String line;
while ((line = reader.readLine()) != null) {
if (line.toUpperCase().startsWith("COLUMNS")) {
int num = Integer.parseInt(line.substring("Columns ".length()));
for (int i = 0; i < num; i++) {
line = reader.readLine().trim();
List<String> cols = Arrays.asList(line.split(","));
if (cols.size() > 0) {
colField.add(cols.get(0).split(" ")[0].trim());
}
}
if (isLoadMifData) {
//装载mif里的geo数据(点,线,面不同类型)
loadMifFileGeoDatas(reader);
}
break;
}
}
reader.close();
} catch (Exception e) {
logger.error("loadMifFile():", e);
}
return colField;
}
//如果geo数据为面
public Map<Integer, List<List<List<Double>>>> loadMifFileGeoDatas(BufferedReader reader) {
Map<Integer, List<List<List<Double>>>> regions = new HashMap<>();
Integer rowIndex = -1;
try {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.toUpperCase().contains("NONE")){
regions.put(++rowIndex, new ArrayList<>());
}
if (line.toUpperCase().startsWith("REGION")) {
int num = Integer.parseInt(line.substring("Region ".length()));
List<List<List<Double>>> dataGroups = new ArrayList<>();
for (int i = 0; i < num; i++) {
line = reader.readLine().trim();
int rowCount = Integer.parseInt(line);
List<List<Double>> datas = new ArrayList<>();
for (int j = 0; j < rowCount; j++) {
line = reader.readLine().trim();
List<String> xy = Arrays.asList(line.split(" "));
List<Double> points = new ArrayList<>();
if (xy.size() == 2) {
points.add(Double.parseDouble(xy.get(0)));
points.add(Double.parseDouble(xy.get(1)));
datas.add(points);
}
}
dataGroups.add(datas);
}
regions.put(++rowIndex, dataGroups);
}
}
reader.close();
} catch (Exception e) {
logger.error("loadMifFileGeoDatas():", e);
}
return regions;
}
public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<List<List<Double>>>> regions,Integer rowIdx) {
if (regions != null) {
List<List<List<Double>>> coordinates = regions.getOrDefault(rowIdx, null);
LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{
put("type", "Polygon");
put("coordinates", coordinates);
}};
return geometryMap;
}
return null;
}
//如果geo数据为点
public Map<Integer, List<Double>> loadMifFileGeoDatas(BufferedReader reader) {
Map<Integer, List<Double>> pointRows = new HashMap<>();
Integer rowIndex = -1;
try {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.toUpperCase().contains("NONE")){
pointRows.put(++rowIndex, new ArrayList<>());
}
if (line.toUpperCase().startsWith("POINT")) {
List<String> point = Arrays.asList(line.split(" "));
if (point.size() == 3) {
List<Double> point1 = new ArrayList<>();
point1.add(Double.parseDouble(point.get(1)));
point1.add(Double.parseDouble(point.get(2)));
pointRows.put(++rowIndex, point1);
}
}
}
reader.close();
} catch (Exception e) {
logger.error("loadMifFileGeoDatas():", e);
}
return pointRows;
}
public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<Double>> pointRows,Integer rowIdx) {
if (pointRows!= null) {
List<Double> pointRow = pointRows.getOrDefault(rowIdx, null);
LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{
put("type", "Point");
put("coordinates", pointRow);
}};
return geometryMap;
}
return null;
}
//如果geo数据为线
public Map<Integer, List<List<Double>>> loadMifFileGeoDatas(BufferedReader reader) {
Map<Integer, List<List<Double>>> lineRows = new HashMap<>();
Integer rowIndex = -1;
try {
String line;
while ((line = reader.readLine()) != null) {
List<List<Double>> lines = new ArrayList<>();
line = line.trim();
if (line.toUpperCase().contains("NONE")){
lineRows.put(++rowIndex, lines);
}
if (line.toUpperCase().startsWith("LINE")) {
List<String> point = Arrays.asList(line.split(" "));
if (point.size() == 5) {
List<Double> point1 = new ArrayList<>();
point1.add(Double.parseDouble(point.get(1)));
point1.add(Double.parseDouble(point.get(2)));
lines.add(point1);
List<Double> point2 = new ArrayList<>();
point2.add(Double.parseDouble(point.get(3)));
point2.add(Double.parseDouble(point.get(4)));
lines.add(point2);
}
lineRows.put(++rowIndex, lines);
}
if (line.startsWith("Pline")) {
int num = Integer.parseInt(line.substring("Pline ".length()));
for (int i = 0; i < num; i++) {
line = reader.readLine().trim();
List<String> point = Arrays.asList(line.split(" "));
if (point.size() == 2) {
List<Double> points = new ArrayList<>();
points.add(Double.parseDouble(point.get(0)));
points.add(Double.parseDouble(point.get(1)));
lines.add(points);
}
}
lineRows.put(++rowIndex, lines);
}
}
reader.close();
} catch (Exception e) {
logger.error("loadMifFileGeoDatas():", e);
}
return lineRows;
}
public LinkedHashMap<String, Object> getGeometryMap(Map<Integer, List<List<Double>>> lineRows,Integer rowIdx) {
if (lineRows!= null) {
List<List<Double>> line = lineRows.getOrDefault(rowIdx, null);
LinkedHashMap<String, Object> geometryMap = new LinkedHashMap<String, Object>() {{
put("type", "LineString");
put("coordinates", line);
}};
return geometryMap;
}
return null;
}