¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.se.simu.config; |
| | | |
| | | import com.se.simu.helper.CaffeineHelper; |
| | | import com.se.simu.helper.GdalHelper; |
| | | import com.se.simu.helper.WebHelper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.gdal.gdal.Band; |
| | | import org.gdal.gdal.Dataset; |
| | | import org.gdal.gdal.Driver; |
| | | import org.gdal.gdal.gdal; |
| | | import org.gdal.gdalconst.gdalconstConstants; |
| | | import org.gdal.ogr.*; |
| | | import org.gdal.osr.SpatialReference; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.boot.ApplicationArguments; |
| | | import org.springframework.boot.ApplicationRunner; |
| | | import org.springframework.core.env.Environment; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | @SuppressWarnings("ALL") |
| | | public class InitConfig implements ApplicationRunner { |
| | | @Resource |
| | | Environment env; |
| | | |
| | | @Value("${server.port}") |
| | | String serverPort; |
| | | |
| | | @Value("${config.cacheTime}") |
| | | Integer cacheTime; |
| | | |
| | | @Value("${server.servlet.context-path}") |
| | | String contextPath; |
| | | |
| | | @Override |
| | | public void run(ApplicationArguments args) { |
| | | // noinspection AlibabaRemoveCommentedCode |
| | | try { |
| | | log.info("***************** åå§å GDAL *****************" + "\n"); |
| | | GdalHelper.init(env.getProperty("config.gdalPath")); |
| | | CaffeineHelper.init(cacheTime); |
| | | |
| | | String path = null != contextPath && contextPath.length() > 1 ? contextPath : ""; |
| | | log.info("APIææ¡£:http://localhost:" + serverPort + path + "/doc.html"); |
| | | log.info("APIææ¡£:http://{}:{}{}/doc.html", WebHelper.getHostIp(), serverPort, path); |
| | | |
| | | log.info("***************** ç³»ç»å¯å¨å®æ¯ *****************" + "\n"); |
| | | |
| | | //log.info("***************** 读åtifæä»¶ *****************" + "\n"); |
| | | // 读åtifæä»¶ |
| | | //readTif("D:\\soft\\env\\tif\\srtm_12_03.tif"); |
| | | //log.info("***************** 读åtifæä»¶å®æ¯ *****************" + "\n"); |
| | | |
| | | //log.info("***************** 读åshpæä»¶ *****************" + "\n"); |
| | | // 读åshpæä»¶ |
| | | //readShp("D:\\soft\\env\\é¨éç«ç¹æ°æ®\\é¨éç«ç¹_84\\é¨éç«ç¹_84.shp"); |
| | | //log.info("***************** 读åshpæä»¶å®æ¯ *****************" + "\n"); |
| | | |
| | | |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 读å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(); |
| | | } |
| | | |
| | | /** |
| | | * 读å Shapefile æä»¶å¹¶æå°å±æ§åå 使°æ® |
| | | * |
| | | * @param shapefilePath SHP æä»¶è·¯å¾ |
| | | */ |
| | | |
| | | public void readShp(String strVectorFile) { |
| | | |
| | | // æ³¨åææçé©±å¨ |
| | | ogr.RegisterAll(); |
| | | // ä¸ºäºæ¯æä¸æè·¯å¾ï¼è¯·æ·»å ä¸é¢è¿å¥ä»£ç |
| | | gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES"); |
| | | // 为äºä½¿å±æ§è¡¨åæ®µæ¯æä¸æï¼è¯·æ·»å ä¸é¢è¿å¥ |
| | | gdal.SetConfigOption("SHAPE_ENCODING", "CP936"); |
| | | // è¯»åæ°æ®ï¼è¿é以ESRIçshpæä»¶ä¸ºä¾ |
| | | String strDriverName = "ESRI Shapefile"; |
| | | // å建ä¸ä¸ªæä»¶ï¼æ ¹æ®strDriverNameæ©å±åèªå¨å¤æé©±å¨ç±»å |
| | | |
| | | org.gdal.ogr.Driver oDriver = ogr.GetDriverByName(strDriverName); |
| | | |
| | | if (oDriver == null) { |
| | | System.out.println(strDriverName + " 驱å¨ä¸å¯ç¨ï¼\n"); |
| | | return; |
| | | } |
| | | DataSource dataSource = oDriver.Open(strVectorFile); |
| | | //Layer layer = dataSource.GetLayer("test"); |
| | | Layer layer = dataSource.GetLayer(0); |
| | | |
| | | for (int i = 0; i < dataSource.GetLayerCount(); i++) { |
| | | Layer layerIdx = dataSource.GetLayer(i); |
| | | System.out.println("å¾å±åç§°ï¼<==>" + layerIdx.GetName()); |
| | | } |
| | | |
| | | 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(); |
| | | |
| | | int fieldCount = featureDefn.GetFieldCount(); |
| | | |
| | | Map<String, String> fieldMap = new HashMap<String, String>(); |
| | | for (int i = 0; i < fieldCount; i++) { |
| | | FieldDefn fieldDefn = featureDefn.GetFieldDefn(i); |
| | | // å¾å°å±æ§å段类å |
| | | int fieldType = fieldDefn.GetFieldType(); |
| | | String fieldTypeName = fieldDefn.GetFieldTypeName(fieldType); |
| | | // å¾å°å±æ§å段åç§° |
| | | String fieldName = fieldDefn.GetName(); |
| | | fieldMap.put(fieldTypeName, fieldName); |
| | | } |
| | | System.out.println(); |
| | | System.out.println("fileMap:"); |
| | | System.out.println(fieldMap); |
| | | |
| | | System.out.println(layer.GetFeature(1).GetGeometryRef().ExportToJson()); |
| | | System.out.println(layer.GetFeature(2).GetGeometryRef().ExportToJson()); |
| | | System.out.println(layer.GetFeature(3).GetGeometryRef().ExportToJson()); |
| | | |
| | | for (int i = 0; i < 12; i++) { |
| | | Feature feature = layer.GetFeature(i); |
| | | Object[] arr = fieldMap.values().toArray(); |
| | | for (int k = 0; k < arr.length; k++) { |
| | | String fvalue = feature.GetFieldAsString(arr[k].toString()); |
| | | System.out.println(" 屿§åç§°:" + arr[k].toString() + ",屿§å¼:" + fvalue); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |