java解析shp文件以及坐标转换(工具类)

百度找了很多大部分都是pom的,maven项目中的,但是用maven下载不了,只能一个jar一个jar下载了,中间也遇到了很多坑,都是pom中没有提到的架包

 

直接上代码,最后我会解析shp文件所用到的所有jar截图粘上

package com.common.utils;

import java.io.File;
import java.io.IOException;
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.apache.commons.lang3.StringUtils;
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.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vividsolutions.jts.geom.Geometry;

/**
 * 解析shp文件
 * @author xujiajia
 *
 */
public class ShapeUtils {

	private static final Logger logger = LoggerFactory.getLogger(ShapeUtils.class);
	/**
	 * 测试
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String shpFilePath = "D:\\YuTuWork\\PT\\河南电信\\文档\\外部矢量数据\\矢量\\shp.shp";
		long time = System.currentTimeMillis();
		List> readFile = readFile(shpFilePath, null);
		long time2 = System.currentTimeMillis();
		System.out.println("耗时:" + (time2 - time));
		System.out.println(readFile);
		
	}
	
	/**
	 * 读取shp文件
	 * 
	 * @Description
	 * @author gxx
	 * @date: 2019年7月3日 上午9:33:43
	 */
	public static List> readFile(String shapePath, String prjPath) {
		List> list = new ArrayList>();
		ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
		ShapefileDataStore dataStore = null;
		try {
			dataStore = (ShapefileDataStore) factory.createDataStore(new File(shapePath).toURI().toURL());
			if (dataStore != null) {
				dataStore.setCharset(Charset.forName("UTF-8"));
			}
			String wkt = "";
			if(StringUtils.isNotBlank(prjPath)) {
				wkt = TxtUtils.readTxtFile(prjPath);
				logger.info("获取到shp文件坐标系:{}", wkt);
			}
			boolean change = false;
			//如果是投影坐标系,则进行坐标转换
			if(wkt.startsWith("PROJCS")) {
				change = true;
			}
			SimpleFeatureSource featureSource = dataStore.getFeatureSource();
			SimpleFeatureIterator itertor = featureSource.getFeatures().features();
//			一个用于处理FeatureCollection的实用工具类。提供一个获取FeatureCollection实例的机制
//      		FeatureCollection result=featureSource.getFeatures();
//      		
//      		FeatureIterator iterator = result.features();
			while (itertor.hasNext()) {
				SimpleFeature feature = itertor.next();
				Iterator it = feature.getProperties().iterator();
				Map map = new HashMap();
				while (it.hasNext()) {
					Property pro = it.next();
					if(change && "the_geom".equals(String.valueOf(pro.getName()))) {
						map.put(String.valueOf(pro.getName()), CoordConverter.convert((Geometry)pro.getValue(), wkt));
					} else {
						map.put(String.valueOf(pro.getName()), String.valueOf(pro.getValue()));
					}
				}
				list.add(map);
//				SimpleFeature feature = (SimpleFeature) iterator.next();
//	            // Feature转GeoJSON
//	            FeatureJSON fjson = new FeatureJSON();
//	            StringWriter writer = new StringWriter();
//	            fjson.writeFeature(feature, writer);
//	            String sjson = writer.toString();
//	            System.out.println("sjson=====  >>>>  "  + sjson);
			}
			itertor.close();
			return list;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			if(dataStore != null) {
				dataStore.dispose();
			}
		}
		return null;
	}

	/**
	 * 丢给从一个文件中获取Shape文件路径
	 *
	 * @param path 文件夹路径
	 * @param path 需要获得的shape文件路径
	 */
	public static void getShapePath(String path, List fielPath) {
		File file = new File(path);
		if (file.exists() && file.isDirectory()) {
			File[] files = file.listFiles();
			for (File file1 : files) {
				if (file1.isDirectory()) {
					getShapePath(file1.getPath(), fielPath);
				} else {
					String fileName = file1.getName();
					String suffix = fileName.substring(fileName.lastIndexOf("."));
					if (".shp".equals(suffix)) {
						fielPath.add(file1.getAbsolutePath());
						break;
					}
				}
			}
		}
	}
	
	
	/**
	 * 获取prj文件路径
	 * @Description
	 * @author gxx
	 * @date: 2019年8月8日 下午6:12:51
	 */
	public static void getPrjPath(String path, List fielPath) {
		File file = new File(path);
		if (file.exists() && file.isDirectory()) {
			File[] files = file.listFiles();
			for (File file1 : files) {
				if (file1.isDirectory()) {
					getShapePath(file1.getPath(), fielPath);
				} else {
					String fileName = file1.getName();
					String suffix = fileName.substring(fileName.lastIndexOf("."));
					if (".prj".equals(suffix)) {
						fielPath.add(file1.getAbsolutePath());
						break;
					}
				}
			}
		}
	}

	
	/**
	 * 获取Shape文件的坐标系信息,GEOGCS表示这个是地址坐标系,PROJCS则表示是平面投影坐标系
	 * @Description
	 * @author gxx
	 * @date: 2019年7月5日 上午9:04:07
	 */
	public static String getCoordinateSystemWKT(String path) {
		ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
		ShapefileDataStore dataStore = null;
		try {
			dataStore = (ShapefileDataStore) factory.createDataStore(new File(path).toURI().toURL());
			return dataStore.getSchema().getCoordinateReferenceSystem().toWKT();
		} catch (UnsupportedOperationException | IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			dataStore.dispose();
		}
		return "";
	}
	

	
	
}

下边的是坐标转换的工具类,在上一个工具中调用到了

package com.common.utils;

import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.io.WKTReader;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author ZhaiYP 坐标转换
 */

/**
 * @author Administrator
 */
public class CoordConverter {

	
	private static final Logger logger = LoggerFactory.getLogger(CoordConverter.class);
	
    /**
     * TODO 墨卡托投影prj文件坐标参数
     */
    final static String strWKTMercator = "PROJCS[\"WGS_1984_UTM_Zone_51N\","
            + "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\"," + "SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],"
            + "PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]," + "PROJECTION[\"Transverse_Mercator\"],"
            + "PARAMETER[\"False_Easting\",500000.0]," + "PARAMETER[\"False_Northing\",0.0],"
            + "PARAMETER[\"Central_Meridian\",123.0]," + "PARAMETER[\"Scale_Factor\",0.9996],"
            + "PARAMETER[\"Latitude_Of_Origin\",0.0]," + "UNIT[\"Meter\",1.0]]\r\n";

   

    /**
     * TODO 墨卡托投影51N投影坐标转换为wgs_84经纬度投影
     *
     * @param lon x坐标
     * @param lat y坐标
     * @return double[] 转换后double类型x/y坐标对
     * @throws FactoryException
     * @throws MismatchedDimensionException
     * @throws TransformException           2018年10月16日
     */
    public static double[] convert_51NToWgs84(double lon, double lat) {
        // 传入原始的经纬度坐标
        Coordinate sourceCoord = new Coordinate(lon, lat);
        GeometryFactory geoFactory = new GeometryFactory();
        Point sourcePoint = geoFactory.createPoint(sourceCoord);
        // 这里是以OGC WKT形式定义的是WGS_1984_UTM_Zone_51N投影
        CoordinateReferenceSystem mercatroCRS;
        Point targetPoint = null;
        try {
            mercatroCRS = CRS.parseWKT(strWKTMercator);
            // CoordinateReferenceSystem wgs_1984 = CRS.parseWKT(wgs_84);
            // 做投影转换,将WCG84坐标转换成世界墨卡托投影转
            MathTransform transform = CRS.findMathTransform(mercatroCRS, DefaultGeographicCRS.WGS84);
            targetPoint = (Point) JTS.transform(sourcePoint, transform);
        } catch (FactoryException e) {
            e.printStackTrace();
        } catch (MismatchedDimensionException e) {
            e.printStackTrace();
        } catch (TransformException e) {
            e.printStackTrace();
        }
        // 返回转换以后的X和Y坐标
        double x = Double.parseDouble(String.format("%.6f", targetPoint.getX()));
        double y = Double.parseDouble(String.format("%.6f", targetPoint.getY()));
        double[] targetCoord = {x, y};
        return targetCoord;
    }

    /**
     * TODO wgs_84经纬度坐标点,转换为墨卡托投影51N坐标点
     *
     * @param lon x坐标
     * @param lat y坐标
     * @return double[] 转换后double类型x/y坐标对
     * @throws FactoryException
     * @throws MismatchedDimensionException
     * @throws TransformException           2018年10月16日
     */
    public static double[] convert_WGS84To51N(double lon, double lat) {
        // 传入原始的经纬度坐标
        Coordinate sourceCoord = new Coordinate(lon, lat);
        GeometryFactory geoFactory = new GeometryFactory();
        Point sourcePoint = geoFactory.createPoint(sourceCoord);
        // 这里是以OGC WKT形式定义的是WGS_1984_UTM_Zone_51N投影
        CoordinateReferenceSystem mercatroCRS;
        Point targetPoint = null;
        try {
            mercatroCRS = CRS.parseWKT(strWKTMercator);
            // CoordinateReferenceSystem wgs_1984 = CRS.parseWKT(wgs_84);
            // 做投影转换,将WCG84坐标转换成世界墨卡托投影转
            MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, mercatroCRS);
            targetPoint = (Point) JTS.transform(sourcePoint, transform);
        } catch (FactoryException e) {
            e.printStackTrace();
        } catch (MismatchedDimensionException e) {
            e.printStackTrace();
        } catch (TransformException e) {
            e.printStackTrace();
        }
        // 返回转换以后的X和Y坐标
        double[] targetCoord = {targetPoint.getX(), targetPoint.getY()};
        return targetCoord;
    }

    
    /**
     * 根据hsp文件坐标系
     * @Description
     * @author gxx
     * @date: 2019年7月5日 上午9:19:27
     */
    public static double[] convert(double lon, double lat, String strWKT) {
        Coordinate sourceCoord = new Coordinate(lon, lat);
        GeometryFactory geoFactory = new GeometryFactory();
        Point sourcePoint = geoFactory.createPoint(sourceCoord);
        CoordinateReferenceSystem mercatroCRS;
        Point targetPoint;
		try {
			mercatroCRS = CRS.parseWKT(strWKT);
			MathTransform transform = CRS.findMathTransform(DefaultGeographicCRS.WGS84, mercatroCRS);
	        targetPoint = (Point) JTS.transform(sourcePoint, transform);
	        double[] targetCoord = {targetPoint.getX(), targetPoint.getY()};
	        return targetCoord;
		} catch (FactoryException | MismatchedDimensionException | TransformException e) {
			logger.error(e.getMessage(), e);
		}
        return null;
    }
    
    /**
     * 根据hsp文件坐标系,将墨卡托投影51N投影坐标转换为wgs_84经纬度投影
     * @Description
     * @author gxx
     * @date: 2019年7月5日 上午9:19:27
     */
    public static String convert(Geometry geom, String strWKT) {
        CoordinateReferenceSystem mercatroCRS;
		try {
			mercatroCRS = CRS.parseWKT(strWKT);
			MathTransform transform = CRS.findMathTransform(mercatroCRS, DefaultGeographicCRS.WGS84);
			Geometry geometry = JTS.transform(geom, transform);
			return geometry.toString();
		} catch (FactoryException | MismatchedDimensionException | TransformException e) {
			logger.error(e.getMessage(), e);
		}
        return null;
    }
    

    public static List> changeCoord(List> list) {
        long startTime = System.currentTimeMillis();
        for (Map map : list) {
            if ((map.get("wgs84_x") == null || "".equals(map.get("wgs84_x")) || "null".equals(map.get("wgs84_x")))
                    && (map.get("bj54_x") != null && !"".equals(map.get("bj54_x")))) {
                String bj54_x = (String) map.get("bj54_x");
                String bj54_y = (String) map.get("bj54_y");
                List bj54_xList = Arrays.asList(bj54_x.split(","));
                List bj54_yList = Arrays.asList(bj54_y.split(","));
                StringBuilder wgs84_x = new StringBuilder();
                StringBuilder wgs84_y = new StringBuilder();
                for (int i = 0; i < bj54_xList.size(); i++) {
                    double x = Double.parseDouble(bj54_xList.get(i));
                    double y = Double.parseDouble(bj54_yList.get(i));
                    double[] wgs84ByBj54 = CoordConverter.convert_51NToWgs84(x, y);
                    String wgs84x = String.valueOf(wgs84ByBj54[0]);
                    String wgs84y = String.valueOf(wgs84ByBj54[1]);
                    wgs84_x.append(wgs84x);
                    wgs84_y.append(wgs84y);
                    if (i < bj54_xList.size() - 1) {
                        wgs84_x.append(",");
                        wgs84_y.append(",");
                    }
                }

                map.put("wgs84_x", wgs84_x.toString());
                map.put("wgs84_y", wgs84_y.toString());
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println(endTime - startTime+ "ms");
        return list;
    }

    // main函数进行验证
    public static void main(String[] args) throws Exception {
        double longitude = 761814.915609849242342;
        double latitude = 4861308.17594982343242;
        long startTime = System.currentTimeMillis();
        double[] coordinate_51N = CoordConverter.convert_51NToWgs84(longitude, latitude);
        long endtime = System.currentTimeMillis();
        System.out.println((endtime - startTime) / 1000);
        System.out.println("X: " + coordinate_51N[0] + ", Y: " + coordinate_51N[1]);
        // double[] coordinate_51N_ = new
        // CoordConverter().convert_51NToWeb(longitude, latitude);
        // System.out.println("X: " + coordinate_51N_[0] + ", Y: " +
        // coordinate_51N_[1]);
        // double[] coordinate_wgs = new
        // CoordConverter().convert_WGS84To51N(coordinate_51N[0],
        // coordinate_51N[1]);
        // System.out.println("X: " + coordinate_wgs[0] + ", Y: " +
        // coordinate_wgs[1]);
        
        
		GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
		WKTReader reader = new WKTReader(geometryFactory);
		String multipolygon = "MULTIPOLYGON (((13749736.970548067 5102563.625129204, 13753483.478041079 5102754.125510205, 13754753.480581086 5100150.620303195, 13753483.478041079 5098118.616239186, 13749038.469151061 5098563.11712819, 13749736.970548067 5102563.625129204)))";
		Geometry geom1 = (Geometry) reader.read(multipolygon);
		String convert = convert(geom1, strWKTMercator);
		System.out.println(convert);
    }
}

TxtUtil工具

 

/**
 * 
 */
package com.common.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;

/**
 * @Description
 * @author xu
 * @date: 2019年8月5日 下午5:26:36
 */
public class TxtUtils {

	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(TxtUtils.class);

	/**
	 * 读取txt文件
	 * 
	 * @Description
	 * @author gxx
	 * @date: 2019年8月5日 下午5:47:11
	 */
	public static String readTxtFile(String path) {
		StringBuilder stringBuilder = new StringBuilder();
		InputStreamReader inputStreamReader = null;
		FileInputStream inputStream = null;
		try {
            inputStream = new FileInputStream(new File(path));
			inputStreamReader = new InputStreamReader(inputStream);
			BufferedReader br = new BufferedReader(inputStreamReader);
			String s = null;
			while ((s = br.readLine()) != null) {
				stringBuilder.append(s);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
			if (inputStreamReader != null) {
				try {
					inputStreamReader.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		return stringBuilder.toString();
	}

	/**
	 * 写入txt文件,覆盖原有内容
	 * 
	 * @Description
	 * @author gxx
	 * @date: 2019年8月5日 下午5:38:02
	 */
	public static void writeTxtFile(String path, String content) {
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(new File(path));
			fileOutputStream.write(content.getBytes());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
	}

	
	
	public static void main(String[] args) {
//		String path = "D:\\uploadFiles\\version.txt";
		String path = "d:\\\\uploadFiles\\\\shp\\\\59bd6477ecbf4f3aa5f62f8dafe77afb\\\\polygon.prj";
		String readTxtFile = readTxtFile(path);
		System.out.println(readTxtFile);
	}
}

所用到的jar(选中的那些)

java解析shp文件以及坐标转换(工具类)_第1张图片

架包下载:https://download.csdn.net/download/xujiahn/12317071(之前设置的0积分,下载的人多了就直接涨到50积分了,我重新设置成0,不到一天就又变成50了,你们就看着截图去下载吧!https://mvnrepository.com/)

java解析shp文件以及坐标转换(工具类)_第2张图片

你可能感兴趣的:(java,工具类,java,shp文件,解析)