Geoserver发布tif图,获取栅格特征值

Geoserver发布tif图,根据经纬度获取栅格特征值

  • 发布方法
    • 发布方法
      • https://blog.csdn.net/u010476739/article/details/78997885
    • 发布可能遇到问题
      • https://www.jianshu.com/p/e97a7f2fb69b
    • 发布后通过不同方式访问接口API
      • https://docs.geoserver.org/stable/en/user/services/index.html#services

代码示例

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.api.test.base.entity.FeatureCollection;
import org.junit.Test;

public class WmsBboxTest {
    @Test
    public void geoInfo(){
		// 经纬度
        double log = 119.18420;
        double lat = 42.34987;
		// 栅格精度
        double offset = 0.00001;
        double minx = log - offset;
        double miny = lat - offset;
        double maxx = log + offset;
        double maxy = lat + offset;
        String path = "http://localhost:8080";
        String layers = "namespace-test:tif-name";
        String url = String.format("%s/geoserver/wms?" +
                "SERVICE=WMS&VERSION=1.3.0" +
                "&REQUEST=GetFeatureInfo" +
                "&FORMAT=application/json" +
                "&TRANSPARENT=true" +
                "&QUERY_LAYERS=%s" +
                "&LAYERS=%s" +
                "&INFO_FORMAT=application/json" +
                "&FEATURE_COUNT=50" +
                "&X=1&" +
                "Y=1" +
                "&WIDTH=1" +
                "&HEIGHT=1"
                + "&BBOX=%s,%s,%s,%s",path, layers, layers, minx ,  miny , maxx ,maxy);
        System.out.println(url);
        HttpResponse execute = HttpUtil.createRequest(Method.GET, url).execute();
        String body = execute.body();
        System.out.println(body);
        FeatureCollection featureCollection = JSONUtil.toBean(body, FeatureCollection.class);
        System.out.println(execute.getStatus());
        System.out.println(featureCollection);
        System.out.println(featureCollection.getFeatures().get(0).getProperties().get("GRAY_INDEX"));
		
//		// 如果设置了访问权限
//		String name = "admin";
//        String password = "geoserver";
//        String authString = name + ":" + password;
//        System.out.println("auth string: " + authString);
//        //Base64编码
//        byte[] authEncBytes = Base64.encodeBase64(authString.getBytes(StandardCharsets.UTF_8));
//        String authStringEnc = new String(authEncBytes);
//        //Authorization格式,注意“Basic ” 有个空格
//        String token = "Basic "+authStringEnc;
//        System.out.println("token: "+token);
//		HttpResponse execute = HttpUtil.createRequest(Method.GET, url)
//                .header(Header.ACCEPT, ConstantData.HEAD_JSON)
//                .header(Header.AUTHORIZATION,token)
//                .execute();
    }
}

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class Feature {
    String type = "Feature";
    Map<String ,Object> properties;
    //Geometry geometry;
    String geometry;
}

@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class FeatureCollection {
    String type = "FeatureCollection";
    Map<String ,Object> properties;
    List<Feature> features;
}

这种方式存在精度问题,经纬度的精度够高和Qgis工具中点中值相同,精度不匹配,会存在差异。

点击阅读全文
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐