geotools画地图
给一串featurecollection 画出地图 对每个geometry上不同的颜色,并在每个geometry的中心点贴上属性
这个需求 没完成
完成到画出地图,给featurecollection上颜色
对每个geometry上不同的颜色应该要将大的featurecollectin拆成多个小的featurecollection 然后 每个featurecollection 设置一个layer颜色
至于在geometry中心点贴上属性完全没找到对应方法 找到的是从shp文件直接导出地图的
先把半成品代码贴上去吧
这个代码从网上抄的
package com.example.testmap;
import org.geotools.feature.FeatureCollection;
import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.map.FeatureLayer;
import org.geotools.map.MapContent;
import org.geotools.map.MapViewport;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.SLD;
import org.geotools.styling.Style;
import org.junit.jupiter.api.Test;
import org.opengis.feature.simple.SimpleFeatureType;
import org.springframework.boot.test.context.SpringBootTest;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Map;
@SpringBootTest
class TestMapApplicationTests {
@Test
void contextLoads() throws IOException {
Path path = Paths.get("C:\\Users\\HTHT\\Desktop\\test.txt");
byte[] data = Files.readAllBytes(path);
String result = new String(data, "utf-8");
System.out.println(result);
System.out.println(result.length());
this.deawMultiPolygonWithHoleFromGeoJson(result);
}
public static void deawMultiPolygonWithHoleFromGeoJson(String json) {
FeatureJSON featureJSON = new FeatureJSON(new GeometryJSON(9));
try {
FeatureCollection featureCollection = featureJSON.readFeatureCollection(json);
System.out.println(featureCollection.getSchema().toString());
// 获取simpleFeatureType
SimpleFeatureType simpleFeatureType = (SimpleFeatureType) featureCollection.getSchema();
try {
// 创建map对象
MapContent map = new MapContent();
Style style = SLD.createLineStyle(new Color(0x1890ff), 2);
Style style1 = SLD.createPolygonStyle(new Color(0x1890ff),new Color(0x3EFF13),0.5f);
FeatureLayer layer = new FeatureLayer(featureCollection, style1,"测试踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩踩从");
// 创建要输出的文件
String saveFile = "C:\\Users\\HTHT\\Desktop\\testtxt.jpg";
// 保存geojson到图片
MapViewport viewPoint = new MapViewport();
ReferencedEnvelope bounds = layer.getBounds();
viewPoint.setBounds(bounds);
map.addLayer(layer);
map.setTitle("cesssssssssssssssssssss");
map.setViewport(viewPoint);
Iterator<Map.Entry<String, Object>> entries = map.getUserData().entrySet().iterator();
while(entries.hasNext()){
Map.Entry<String, Object> entry = entries.next();
String mapKey = entry.getKey();
String mapValue = entry.getValue().toString();
System.out.println(mapKey+":"+mapValue);
}
saveMapToImage(map, saveFile, 1000);
} catch (Exception e){
}
}catch (Exception e){
}
}
public static void saveMapToImage(final MapContent map, final String file, final int imageWidth) {
GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
Rectangle imageBounds = null;
ReferencedEnvelope mapBounds = null;
try {
mapBounds = map.getMaxBounds();
double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
imageBounds = new Rectangle(0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
} catch (Exception e) {
// failed to access map layers
throw new RuntimeException(e);
}
BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D gr = image.createGraphics();
gr.setPaint(Color.WHITE);
gr.fill(imageBounds);
gr.drawString("test",3,5);
gr.setBackground(Color.red);
try {
renderer.paint(gr, imageBounds, mapBounds);
File fileToSave = new File(file);
ImageIO.write(image, "JPEG", fileToSave);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void saveImage(final MapContent map, final String file, final int imageWidth) {
GTRenderer renderer = new StreamingRenderer();
renderer.setMapContent(map);
Rectangle imageBounds = null;
ReferencedEnvelope mapBounds = null;
try {
mapBounds = map.getMaxBounds();
double heightToWidth = mapBounds.getSpan(1) / mapBounds.getSpan(0);
imageBounds = new Rectangle(
0, 0, imageWidth, (int) Math.round(imageWidth * heightToWidth));
} catch (Exception e) {
// failed to access map layers
throw new RuntimeException(e);
}
BufferedImage image = new BufferedImage(imageBounds.width, imageBounds.height, BufferedImage.TYPE_INT_RGB);
Graphics2D gr = image.createGraphics();
gr.setPaint(Color.WHITE);
gr.fill(imageBounds);
try {
renderer.paint(gr, imageBounds, mapBounds);
File fileToSave = new File(file);
ImageIO.write(image, "JPEG", fileToSave);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
那个pom依赖比较麻烦
贴个pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>testMap</artifactId> <version>0.0.1-SNAPSHOT</version> <name>testMap</name> <description>testMap</description> <properties> <java.version>8</java.version> </properties> <repositories> <repository> <id>osgeo</id> <name>OSGeo Release Repository</name> <url>https://repo.osgeo.org/repository/release/</url> <snapshots><enabled>false</enabled></snapshots> <releases><enabled>true</enabled></releases> </repository> <repository> <id>osgeo-snapshot</id> <name>OSGeo Snapshot Repository</name> <url>https://repo.osgeo.org/repository/snapshot/</url> <snapshots><enabled>true</enabled></snapshots> <releases><enabled>false</enabled></releases> </repository> </repositories> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.github.tanhuang2016</groupId> <artifactId>jmap</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.73</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-shapefile</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-api</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geojson</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-geometry</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-jts-wrapper</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-main</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-epsg-hsql</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-opengis</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-data</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-referencing</artifactId> <version>20.3</version> </dependency> <dependency> <groupId>org.geotools</groupId> <artifactId>gt-swt</artifactId> <version>20.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>

浙公网安备 33010602011771号