geotools画地图

给一串featurecollection  画出地图  对每个geometry上不同的颜色,并在每个geometry的中心点贴上属性

 

这个需求  没完成

完成到画出地图,给featurecollection上颜色

 

对每个geometry上不同的颜色应该要将大的featurecollectin拆成多个小的featurecollection  然后 每个featurecollection 设置一个layer颜色

至于在geometry中心点贴上属性完全没找到对应方法  找到的是从shp文件直接导出地图的

先把半成品代码贴上去吧

这个代码从网上抄的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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>
复制代码

 

posted @   霸王龙168  阅读(290)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示