¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.lf.server.helper; |
| | | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.gdal.Band; |
| | | import org.gdal.gdal.Driver; |
| | | import org.gdal.gdal.gdal; |
| | | import org.gdal.gdal.Dataset; |
| | | import org.gdal.gdalconst.gdalconstConstants; |
| | | import org.gdal.ogr.*; |
| | | import org.gdal.osr.SpatialReference; |
| | | |
| | | import java.sql.Time; |
| | | import java.sql.Timestamp; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * GDAL帮å©ç±» |
| | | * @author WWW |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | public class GdalHelper { |
| | | private final static Log log = LogFactory.getLog(GdalHelper.class); |
| | | |
| | | public static void init() { |
| | | // æ³¨åææçé©±å¨ |
| | | gdal.AllRegister(); |
| | | |
| | | // ä¸ºäºæ¯æä¸æè·¯å¾ï¼è¯·æ·»å ä¸é¢è¿å¥ä»£ç |
| | | gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); |
| | | |
| | | // é
ç½®GDAL_DATAè·¯å¾ï¼gdalæ ¹ç®å½ä¸çbin\gdal-dataï¼ |
| | | // gdal.SetConfigOption("GDAL_DATA", "E:\\terrait\\TianJin\\Zip\\release-1928-x64-dev\\release-1928-x64\\bin\\gdal-data"); |
| | | // gdal.SetConfigOption("PROJ_LIB", "E:\\terrait\\TianJin\\Zip\\release-1928-x64-dev\\release-1928-x64\\bin\\proj7\\share"); |
| | | |
| | | // 为äºä½¿å±æ§è¡¨åæ®µæ¯æä¸æï¼è¯·æ·»å ä¸é¢è¿å¥ï¼CP936 |
| | | gdal.SetConfigOption("SHAPE_ENCODING", ""); |
| | | } |
| | | |
| | | /** |
| | | * 读åtifæä»¶ |
| | | * |
| | | * @param fileName |
| | | */ |
| | | public static void readTif(String fileName) { |
| | | // 读åå½±åæ°æ® |
| | | Dataset dataset = gdal.Open(fileName, gdalconstConstants.GA_ReadOnly); |
| | | if (dataset == null) { |
| | | System.out.println("read fail!"); |
| | | return; |
| | | } |
| | | |
| | | // providing various methods for a format specific driver. |
| | | Driver driver = dataset.GetDriver(); |
| | | System.out.println("driver name: " + driver.getLongName()); |
| | | |
| | | // 读åå½±åä¿¡æ¯ |
| | | int xSize = dataset.getRasterXSize(); |
| | | int ySzie = dataset.getRasterYSize(); |
| | | int rasterCount = dataset.getRasterCount(); |
| | | System.out.println("dataset xSize: " + xSize + ", ySzie = " + ySzie + ", rasterCount = " + rasterCount); |
| | | |
| | | Band band = dataset.GetRasterBand(1); |
| | | // the data type of the band. |
| | | int type = band.GetRasterDataType(); |
| | | System.out.println("data type = " + type + ", " + (type == gdalconstConstants.GDT_Byte)); |
| | | |
| | | // Frees the native resource associated to a Dataset object and close the file. |
| | | dataset.delete(); |
| | | |
| | | gdal.GDALDestroyDriverManager(); |
| | | } |
| | | |
| | | /** |
| | | * 读åshpæä»¶ |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readShp(String filePath) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("ESRI shapefile"); |
| | | if (driver == null) { |
| | | System.out.println(" ESRI shapefile驱å¨ä¸å¯ç¨ï¼\n"); |
| | | System.out.println("fail"); |
| | | } |
| | | |
| | | DataSource dataSource = driver.Open(filePath); |
| | | Layer layer = dataSource.GetLayer(0); |
| | | |
| | | String layerName = layer.GetName(); |
| | | System.out.println("å¾å±åç§°ï¼" + layerName); |
| | | |
| | | SpatialReference spatialReference = layer.GetSpatialRef(); |
| | | // System.out.println(spatialReference) |
| | | System.out.println("空é´åèåæ ç³»ï¼" + spatialReference.GetAttrValue("AUTHORITY", 0) + spatialReference.GetAttrValue("AUTHORITY", 1)); |
| | | |
| | | double[] layerExtent = layer.GetExtent(); |
| | | System.out.println("å¾å±èå´ï¼minx:" + layerExtent[0] + ",maxx:" + layerExtent[1] + ",miny:" + layerExtent[2] + ",maxy:" + layerExtent[3]); |
| | | |
| | | FeatureDefn featureDefn = layer.GetLayerDefn(); |
| | | Map<String, Object> fieldMap = new HashMap(5); |
| | | for (int i = 0, count = featureDefn.GetFieldCount(); i < count; i++) { |
| | | FieldDefn fieldDefn = featureDefn.GetFieldDefn(i); |
| | | //å¾å°å±æ§å段类å |
| | | int fieldType = fieldDefn.GetFieldType(); |
| | | String fieldTypeName = fieldDefn.GetFieldTypeName(fieldType); |
| | | //å¾å°å±æ§å段åç§° |
| | | String fieldName = fieldDefn.GetName(); |
| | | |
| | | fieldMap.put(fieldTypeName, fieldName); |
| | | } |
| | | |
| | | long featureCount = layer.GetFeatureCount(); |
| | | System.out.println("å¾å±è¦ç´ 个æ°ï¼" + featureCount); |
| | | for (int i = 0; i < featureCount; i++) { |
| | | Feature feature = layer.GetFeature(i); |
| | | Object[] arr = fieldMap.values().toArray(); |
| | | for (int k = 0; k < arr.length; k++) { |
| | | String value = feature.GetFieldAsString(arr[k].toString()); |
| | | System.out.print(" 屿§åç§°:" + arr[k].toString() + ",屿§å¼:" + value); |
| | | } |
| | | System.out.println(); |
| | | break; |
| | | } |
| | | } catch (Exception ex) { |
| | | ex.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | public static void getLayerDto(String filePath) { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | if (driver == null) { |
| | | return; |
| | | } |
| | | |
| | | List<Map> list = new ArrayList<>(); |
| | | List<Map<String, String>> mapList = new ArrayList<>(); |
| | | DataSource dataSource = null; |
| | | try { |
| | | dataSource = driver.Open(filePath, 0); |
| | | int num = dataSource.GetLayerCount(); |
| | | for (int i = 0; i < num; i++) { |
| | | // è·åå¾å± |
| | | Layer layer = dataSource.GetLayer(i); |
| | | // 表åç§° |
| | | String strlayerName = layer.GetName(); |
| | | // è·åå¾å±è¦æ°ä¸ªæ° |
| | | long count = layer.GetFeatureCount(); |
| | | if (0 != count) { |
| | | do { |
| | | // è·åå¾å±ä¸çè¦ç´ |
| | | Feature feature = layer.GetNextFeature(); |
| | | if (null == feature) { |
| | | break; |
| | | } |
| | | // è·åè¾¹çåæ |
| | | Geometry geometry = feature.GetGeometryRef(); |
| | | if (geometry != null) { |
| | | String geometryJson = geometry.ExportToJson(); |
| | | String str = geometryJson.substring(geometryJson.lastIndexOf(":") + 1, geometryJson.length()); |
| | | if (!" [ [ ] ] }".equals(str)) { |
| | | String s1 = str.substring(7, str.length() - 8); |
| | | String[] split = s1.replaceAll(" ", "").replaceAll("\\[", "").replaceAll("]", "").split(","); |
| | | List<String> xList = new ArrayList(); |
| | | List<String> yList = new ArrayList(); |
| | | Map<String, String> map1 = new HashMap(5); |
| | | for (int j = 0; j < split.length; j++) { |
| | | if (j != (split.length - 1) && j % 2 == 0) { |
| | | map1.put(split[j], split[j + 1]); |
| | | } |
| | | } |
| | | mapList.add(map1); |
| | | } |
| | | } |
| | | Map map = new HashMap(5); |
| | | //è·å屿§ |
| | | for (int p = 0; p < feature.GetFieldCount(); p++) { |
| | | map.put(feature.GetFieldDefnRef(p).GetName(), getProperty(feature, p)); |
| | | } |
| | | list.add(map); |
| | | feature.delete(); |
| | | } while (true); |
| | | } |
| | | //layerDto.setLayerName(strlayerName); |
| | | //layerDto.setList(list); |
| | | //layerDto.setCount(count); |
| | | //layerDto.setMap(mapList); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | if (dataSource != null) { |
| | | dataSource.delete(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读ågdbæä»¶ |
| | | * |
| | | * @param filePath |
| | | */ |
| | | public static void readGdb(String filePath) { |
| | | try { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | if (driver == null) { |
| | | return; |
| | | } |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | |
| | | for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) { |
| | | // è·åå¾å± |
| | | Layer layer = dataSource.GetLayer(i); |
| | | System.out.println(i + "\t" + layer.GetName()); |
| | | do { |
| | | // è·åå¾å±ä¸çè¦ç´ |
| | | Feature feature = layer.GetNextFeature(); |
| | | if (null == feature) { |
| | | break; |
| | | } |
| | | // è·åæ ·å¼ï¼è¿éæ¯ç©ºï¼å¾
ç¡®å® |
| | | System.out.println(feature.GetStyleString()); |
| | | // è·ågeometryï¼ä¹å¯ä»¥ExportToWkb() gmlçç |
| | | System.out.println(feature.GetGeometryRef().ExportToWkt()); |
| | | // è·å屿§ |
| | | System.out.println("----------------------------------"); |
| | | for (int p = 0; p < feature.GetFieldCount(); p++) { |
| | | System.out.println(feature.GetFieldDefnRef(p).GetName() + "\t" + getProperty(feature, p)); |
| | | } |
| | | System.out.println("----------------------------------"); |
| | | break; |
| | | } while (true); |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | public static Object getProperty(Feature feature, int index) { |
| | | int type = feature.GetFieldType(index); |
| | | |
| | | PropertyGetter propertyGetter; |
| | | if (type < 0 || type >= PROPERTY_GETTERS.length) { |
| | | propertyGetter = STRING_PROPERTY_GETTER; |
| | | } else { |
| | | propertyGetter = PROPERTY_GETTERS[type]; |
| | | } |
| | | |
| | | try { |
| | | return propertyGetter.get(feature, index); |
| | | } catch (Exception ex) { |
| | | // ex.printStackTrace() |
| | | log.error(ex.getMessage(), ex); |
| | | |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 屿§è·åå¨ |
| | | */ |
| | | @FunctionalInterface |
| | | private interface PropertyGetter { |
| | | /** |
| | | * è·å屿§ |
| | | * |
| | | * @param feature |
| | | * @param index |
| | | * @return |
| | | */ |
| | | Object get(Feature feature, int index); |
| | | } |
| | | |
| | | private static final PropertyGetter STRING_PROPERTY_GETTER = (feature, index) -> feature.GetFieldAsString(index); |
| | | |
| | | /** |
| | | * feature.GetFieldType(index)å¾å°ä¸ä¸ªå±æ§ç±»åçintå¼,该å¼å¯¹åºå
·ä½ç±»å |
| | | */ |
| | | private static final PropertyGetter[] PROPERTY_GETTERS = new PropertyGetter[]{ |
| | | // 0-Integer |
| | | (feature, index) -> feature.GetFieldAsInteger(index), |
| | | // 1-IntegerList |
| | | (feature, index) -> feature.GetFieldAsIntegerList(index), |
| | | // 2-Real |
| | | (feature, index) -> feature.GetFieldAsDouble(index), |
| | | // 3-RealList |
| | | (feature, index) -> feature.GetFieldAsDoubleList(index), |
| | | // 4-String |
| | | STRING_PROPERTY_GETTER, |
| | | // 5-StringList |
| | | (feature, index) -> feature.GetFieldAsStringList(index), |
| | | // 6-unknown |
| | | STRING_PROPERTY_GETTER, |
| | | // 7-unknown |
| | | STRING_PROPERTY_GETTER, |
| | | // 8-Binary |
| | | (feature, index) -> feature.GetFieldAsBinary(index), |
| | | // 9-Date |
| | | (feature, index) -> { |
| | | int[] pnYear = new int[1]; |
| | | int[] pnMonth = new int[1]; |
| | | int[] pnDay = new int[1]; |
| | | int[] pnHour = new int[1]; |
| | | int[] pnMinute = new int[1]; |
| | | float[] pfSecond = new float[1]; |
| | | int[] pnTZFlag = new int[1]; |
| | | feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag); |
| | | java.sql.Date date = java.sql.Date.valueOf(LocalDate.of(pnYear[0], pnMonth[0], pnDay[0])); |
| | | return date; |
| | | }, |
| | | // 10-Time |
| | | (feature, index) -> { |
| | | int[] pnYear = new int[1]; |
| | | int[] pnMonth = new int[1]; |
| | | int[] pnDay = new int[1]; |
| | | int[] pnHour = new int[1]; |
| | | int[] pnMinute = new int[1]; |
| | | float[] pfSecond = new float[1]; |
| | | int[] pnTZFlag = new int[1]; |
| | | feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag); |
| | | float fSecond = pfSecond[0]; |
| | | int s = (int) fSecond; |
| | | int ns = (int) (1000000000 * fSecond - s); |
| | | Time time = Time.valueOf(LocalTime.of(pnHour[0], pnMinute[0], s, ns)); |
| | | return time; |
| | | }, |
| | | // 11-DateTime |
| | | (feature, index) -> { |
| | | int[] pnYear = new int[1]; |
| | | int[] pnMonth = new int[1]; |
| | | int[] pnDay = new int[1]; |
| | | int[] pnHour = new int[1]; |
| | | int[] pnMinute = new int[1]; |
| | | float[] pfSecond = new float[1]; |
| | | int[] pnTZFlag = new int[1]; |
| | | feature.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTZFlag); |
| | | float fSecond = pfSecond[0]; |
| | | int s = (int) fSecond; |
| | | int ns = (int) (1000000000 * fSecond - s); |
| | | LocalDateTime localDateTime = LocalDateTime.of( |
| | | LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]), |
| | | LocalTime.of(pnHour[0], pnMinute[0], s, ns) |
| | | ); |
| | | Timestamp timestamp = Timestamp.valueOf(localDateTime); |
| | | return timestamp; |
| | | }, |
| | | // 12-Integer64 |
| | | (feature, index) -> feature.GetFieldAsInteger64(index), |
| | | // 13 Integer64List |
| | | (feature, index) -> feature.GetFieldAsIntegerList(index) |
| | | // >=14 (unknown) |
| | | }; |
| | | } |