| | |
| | | <artifactId>proj4j</artifactId> |
| | | <version>0.1.0</version> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>org.apache.commons</groupId> |
| | | <artifactId>commons-imaging</artifactId> |
| | | <version>1.0-alpha2</version> |
| | | </dependency> |
| | | </dependencies> |
| | | <repositories> |
| | | <repository> |
| | |
| | | fileWriter.close(); |
| | | return success(pointPath); |
| | | } |
| | | |
| | | @ApiOperation(value = "å°å½¢è½¬cityjson") |
| | | @GetMapping(value = "/terrainToCityJson", produces = "application/json; charset=UTF-8") |
| | | public R<String> terrainToCityJson(@RequestParam("tifPath") String tifPath) throws Exception { |
| | | long times = System.currentTimeMillis(); |
| | | String path = outPath + "\\terrain\\"+times+"\\"; |
| | | File dirFile = new File(path+"appearance"); |
| | | if (!dirFile.exists()) { |
| | | dirFile.mkdirs(); |
| | | } |
| | | String pngPath=path+"appearance\\terrain.png"; |
| | | TiffToRGBUtil.tiffToPng(tifPath,pngPath); |
| | | JSONObject jsonObject = getModule("terrainmodule.json"); |
| | | jsonObject.put("vertices",TiffCoordinateExtractorUtil.getCoordinate(tifPath)); |
| | | FileWriter fileWriter = new FileWriter(path + "terrain.json"); |
| | | fileWriter.write(jsonObject.toJSONString()); |
| | | fileWriter.close(); |
| | | return success(path); |
| | | } |
| | | public JSONObject getModule(String moduleName) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | try { |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.se.simu.utils; |
| | | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import org.gdal.gdal.Dataset; |
| | | import org.gdal.gdal.gdal; |
| | | |
| | | public class TiffCoordinateExtractorUtil { |
| | | public static void main(String[] args) { |
| | | getCoordinate("D:\\åå¸å
æ¶\\sem\\DEM.tif"); |
| | | } |
| | | public static JSONArray getCoordinate(String tifPath){ |
| | | gdal.AllRegister(); |
| | | JSONArray array=new JSONArray(); |
| | | Dataset dataset = gdal.Open(tifPath); |
| | | if (dataset!= null) { |
| | | double[] geotransform = dataset.GetGeoTransform(); |
| | | if (geotransform!= null) { |
| | | double xmin = geotransform[0]; |
| | | double ymax = geotransform[3]; |
| | | double xmax = geotransform[0] + geotransform[1] * dataset.getRasterXSize(); |
| | | double ymin = geotransform[3] + geotransform[5] * dataset.getRasterYSize(); |
| | | System.out.println("å·¦ä¸è§ç»çº¬åº¦: (" + xmin + ", " + ymax + ")"); |
| | | array.add(ProjectionToGeographicUtil.getPoint(xmin,ymax)); |
| | | System.out.println("å³ä¸è§ç»çº¬åº¦: (" + xmax + ", " + ymax + ")"); |
| | | array.add(ProjectionToGeographicUtil.getPoint(xmax,ymax)); |
| | | System.out.println("å·¦ä¸è§ç»çº¬åº¦: (" + xmin + ", " + ymin + ")"); |
| | | array.add(ProjectionToGeographicUtil.getPoint(xmin,ymin)); |
| | | System.out.println("å³ä¸è§ç»çº¬åº¦: (" + xmax + ", " + ymin + ")"); |
| | | array.add(ProjectionToGeographicUtil.getPoint(xmax,ymin)); |
| | | } |
| | | } |
| | | System.out.println(array.toJSONString()); |
| | | return array; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.se.simu.utils; |
| | | |
| | | import org.apache.commons.imaging.*; |
| | | |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | |
| | | public class TiffToRGBUtil { |
| | | |
| | | public static void tiffToPng(String tifPath, String pngPath) throws Exception { |
| | | // è¾åºç PNG æä»¶è·¯å¾ |
| | | String outputPngFilePath = pngPath; |
| | | try { |
| | | // 读å TIF å¾å |
| | | File tiffFile = new File(tifPath); |
| | | BufferedImage tifImage = Imaging.getBufferedImage(tiffFile); |
| | | int width = tifImage.getWidth(); |
| | | int height = tifImage.getHeight(); |
| | | // å建ä¸ä¸ªæ°ç BufferedImage 对象ï¼ç±»å为 TYPE_INT_ARGB |
| | | BufferedImage pngImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); |
| | | // éå TIF å¾åçæ¯ä¸ªåç´ |
| | | for (int y = 0; y < height; y++) { |
| | | for (int x = 0; x < width; x++) { |
| | | // è·å TIF å¾åä¸å½ååç´ çé¢è²å¼ |
| | | int pixel = tifImage.getRGB(x, y); |
| | | // å¨è¿éå¯ä»¥å¯¹åç´ çé¢è²å¼è¿è¡ä¸äºå¤çï¼ä¾å¦æ ¹æ®é«åº¦ä¿¡æ¯è°æ´é¢è² |
| | | // å设æä»¬ç®åå°å°åç´ ç红è²åéæ ¹æ®é«åº¦è¿è¡è°æ´ |
| | | int red = (pixel >> 16) & 0xff; |
| | | int green = (pixel >> 8) & 0xff; |
| | | int blue = (pixel) & 0xff; |
| | | // å设é«åº¦ä¿¡æ¯åå¨å¨ç»¿è²åéä¸ï¼ä½ å¯ä»¥æ ¹æ®å®é
æ
åµè°æ´ |
| | | //height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1) |
| | | int heightValue = (int) (-10000 + ((red * 256 * 256 + green * 256 + blue) * 0.1)); |
| | | System.out.println(heightValue); |
| | | // ç®åå°å°çº¢è²åéæ ¹æ®é«åº¦å¼è¿è¡è°æ´ï¼ä¾å¦ï¼è¶é«è¶çº¢ |
| | | // ç¡®ä¿çº¢è²åéä¸è¶
è¿ 255 |
| | | //red = Math.min((int) (red + (heightValue * 0.5)), 255); |
| | | // éæ°ç»å ARGB å¼ |
| | | Color color = new Color(red, green, blue); |
| | | System.out.printf("Pixel (%d, %d): R=%d, G=%d, B=%d%n", x, y, red, green, blue); |
| | | int newRgb = color.getRGB(); |
| | | // å°å¤çåçåç´ é¢è²å¼è®¾ç½®å°æ°ç PNG å¾åä¸ |
| | | pngImage.setRGB(x, y, newRgb); |
| | | } |
| | | } |
| | | // ä¿å为 PNG å¾å |
| | | ImageIO.write(pngImage, "png", new File(outputPngFilePath)); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | {"type":"CityJSON","version":"1.0","CityObjects":{"UUID_63b11762-6a6a-4d91-b497-6314fee1ce0f":{"type":"+Terrain","geometry":[{"type":"MultiSurface","boundaries":[[[0,1,2,3]]],"texture":{"visual":{"values":[[[0,0,1,2,3]]]}},"lod":0}]}},"appearance":{"textures":[{"type":"PNG","image":"appearances/terrain.png"}],"vertices-texture":[[0.0,0.0],[0.0,1.0],[1.0,1.0],[1.0,0.0]]}} |