首页 > 其他分享 >geotools画地图

geotools画地图

时间:2023-02-22 11:23:37浏览次数:39  
标签:map geotools boot org 地图 new import

给一串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>

 

标签:map,geotools,boot,org,地图,new,import
From: https://www.cnblogs.com/bawanglong168/p/17143700.html

相关文章

  • m基于GA遗传优化的AGV栅格地图路径规划和避障matlab仿真
    1.算法描述        遗传算法GA把问题的解表示成“染色体”,在算法中也即是以二进制编码的串。并且,在执行遗传算法之前,给出一群“染色体”,也即是假设解。然后,把这些......
  • 通过使用java获取地图切片
    主要为了腾讯地图PictureCut.javapublicclassPictureCut{/***@paramsrcImageFile源图像地址*@paramdescImageFile切片目标文件夹*@pa......
  • 微信小程序:地图组件入门
    一、入门案例新建一个页面,在wxml文件中编写下面的代码<map/>效果如下:发现此时地点在北京。我们设置一下地图的宽和高<mapstyle="width:100%;height:600rpx;"/......
  • 百度地图上的标注物太多导致界面卡顿的解决办法
    百度地图的API虽然说覆盖物多了可以用聚合,但聚合使用下来,性能并不好目前解决方案是,获取地图的左下角和右上角的经纬度,然后根据此经纬度范围,到数据库中搜索,把该区域内的覆盖......
  • 百度地图点击获取经纬度,地点名称,标注
    功能介绍1,自动定位当前位置;(可能要等一会儿)2,搜索功能3,点击地图小红点标记,4,点击地图获取当前位置,和当前坐标代码:​​​https://github.com/liaoshengping/myJsNote/blob......
  • Android 高德地图绘制虚线线条
    1.获得绘制虚线的坐标点集合//绘制虚线的gps坐标点privateList<LatLng>convesLngs=newArrayList<LatLng>();/***扇形区域折线存储*/privateMap<Stri......
  • Android 高德地图自定义Marker覆盖物
    直接贴代码:privatevoidquernResCircle(LatLnglatLng,Stringtitle,Stringaddress){ViewmarkerView=ViewGroup.inflate(LocationActivity.this,R.layout.......
  • 城市地图-- 图的深度优先搜素
    #include<iostream>#include<algorithm>#include<cstdio>usingnamespacestd;intmindis=9999999;constintMAX=1000;intn,m;intbook[MAX];intmap[MAX][MA......
  • vue中的echarts地图
    地图万能模板+地图配置+全国数据+地图重绘0.echarts图表只能在mounted生命周期函数调用,created钩子中页面还没挂载1、样例that.chart0=this.$echarts.init(document......
  • Cocos2D 地图制作,并让小人沿着路行走
    把map.tmx与原图bk1.jpg拷贝的工程的assert目录下9.加载地图CCTMXTiledMapmap=CCTMXTiledMap.tiledMap(“map.tmx”);10.在titled里给地图添加对象层效果图,重新拷贝到......