首页 > 编程语言 >java实体类转化geojson的工具类

java实体类转化geojson的工具类

时间:2024-01-22 10:14:03浏览次数:31  
标签:实体类 java String JSONObject object geojson geomFildName properties

1.用到的技术、工具:反射+geotools

2.代码实现

package org.jeecg.modules.web.util.geoutils;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.geotools.geometry.jts.WKBReader;

import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * java实体类转化geojson工具类
 *
 * @Author ergui
 * @Package:org.jeecg.modules.web.util.geoutils
 * @Project:zg-server
 * @name:aa
 * @Date:2024/1/19 15:02
 * @Filename:Entity2GeojsonUtil
 **/
public class Entity2GeojsonUtil {

    /**
     * 实体集合转geojson
     *
     * @param objects    原实体类
     * @param properties 需要在geojson中展示的属性
     * @return geojson
     * @throws IllegalAccessException
     */
    public static String listObject2Geojson(List objects, String geomFildName, String... properties) {
        JSONObject result = new JSONObject();
        result.putOnce("type", "FeatureCollection");
        JSONArray features = new JSONArray();
        objects.forEach(object -> {
            JSONObject geojson = null;
            geojson = object2Feature(object, geomFildName, properties);
            features.add(geojson);
        });
        result.putOnce("features", features);
        return result.toString();
    }

    /**
     * 实体转geojson
     *
     * @param object       原实体类
     * @param geomFildName 空间属性字段名、可为空,默认为geom
     * @param properties   需要在geojson中展示的属性
     * @return geojson
     * @throws IllegalAccessException
     */

    public static String singleObject2Geojson(Object object, @Nullable String geomFildName, String... properties) {
        JSONObject result = new JSONObject();
        result.putOnce("type", "FeatureCollection");
        JSONArray features = new JSONArray();
        JSONObject geojson = object2Feature(object, geomFildName, properties);
        features.add(geojson);
        result.putOnce("features", features);
        return result.toString();
    }

    /**
     * 实体转完整的FeatureJson()
     *
     * @param object     原实体类
     * @param properties 需要在geojson中展示的属性
     * @return geojson
     * @throws IllegalAccessException
     */
    public static JSONObject object2Feature(Object object, String geomFildName, String... properties) {
        JSONObject result = new JSONObject();
        result.putOnce("type", "Feature");
        result.putOnce("id", "xxx");
        // 获取实体中所有属性字段
        JSONObject feature = generateFeature(object, geomFildName);
        result.putOnce("geometry", feature);
        /*属性转properties*/
        JSONObject newproperties = generateProperties(object, geomFildName, properties);
        result.putOnce("properties", newproperties);
        return result;
    }

    /**
     * 生成Feature
     *
     * @param object
     * @param geomFildName 空间属性字段名
     * @return
     */
    private static JSONObject generateFeature(Object object, String geomFildName) {
        Field[] fields = ReflectUtil.getFields(object.getClass());
        String geomString = "";
        if (fields != null && fields.length > 0) {
            Optional<Field> geom = Arrays.stream(fields).filter(f -> f.getName().contains(StrUtil.isEmpty(geomFildName) ?
                    "geom" : geomFildName)).findFirst();
            if (geom.isPresent()) {
                Field geomField = geom.get();
                //如果是私有属性,需要设置属性的可访问性,不设置的话,会抛出异常。
                geomField.setAccessible(true);
                try {
                    geomString = (String) geomField.get(object);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        JSONObject feature = null;
        if (StrUtil.isEmpty(geomString)) {
            throw new RuntimeException("未解析到有效的空间字段");
        }
        /*空间属性值为wkt格式*/
        if (GeometryDataCoverUtils.isWkt(geomString)) {
            feature = GeometryDataCoverUtils.wktToGeoJson(geomString);
            return feature;
        }
        /*空间属性值为GeoJson格式*/
        if (GeometryDataCoverUtils.isGeoJson(geomString)) {
            feature = JSONUtil.parseObj(geomString);
            return feature;
        }
        /*空间属性值为wkb格式*/
        byte[] bytes = WKBReader.hexToBytes(geomString);
        if (GeometryDataCoverUtils.isValidWKB(bytes)) {
            String geojson = GeometryDataCoverUtils.wkb2Geojson(bytes);
            feature = JSONUtil.parseObj(geojson);
        }
        return feature;
    }

    /**
     * 生成properties
     *
     * @param object
     * @param fields
     * @return
     * @throws IllegalAccessException
     */
    private static JSONObject generateProperties(Object object, String geomFildName, String... fields) {
        JSONObject properties = new JSONObject();
        Field[] allFields = ReflectUtil.getFields(object.getClass());
        try {
            if (fields != null && fields.length > 0) {
                for (int i = 0; i < fields.length; i++) {
                    String field1 = fields[i];
                    Field field = Arrays.stream(allFields).filter(f -> f.getName().equals(field1)).findFirst().get();
                    field.setAccessible(true);
                    properties.put(field.getName(), field.get(object));
                }
            } else {
                for (int i = 0; i < allFields.length; i++) {
                    Field field = allFields[i];
                    if (!StrUtil.isEmpty(geomFildName)) {
                        if (!field.getName().contains(geomFildName)) {
                            field.setAccessible(true);
                            properties.put(field.getName(), field.get(object));
                        }
                    } else if (!field.getName().contains("geom")) {
                        field.setAccessible(true);
                        properties.put(field.getName(), field.get(object));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return properties;
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.setAa("aaa");
        test.setType("1111");
        //test.setGeom("01060000208A110000010000000103000000010000008A000000308767204E405B40A0582CBA3F324140E012FEB44D405B40D0E25E963F324140688443B14D405B4008F93FAB3F32414000E63D154A3F5B406017B9D2EB314140CC15B41C4A3F5B4010258A98EB31414094CF870E313F5B4010069677E331414028B9D107183F5B40A0856522DB314140B8408D0BFF3E5B40A8C04169D23141403CB5B2CBF73E5B40182AFDC6CF314140A4B883DBF13E5B4080359F91CD314140E8CCA976E33E5B40E0B15F17C8314140EC594874DA3E5B4050B8DF8DC4314140F0E7297FD23E5B404070675CC1314140ACDA79ABC63E5B4030BDC97EBC31414080DA7413BB3E5B4018EBBD93B73141401863ECC6B33E5B407074B669B431414008F53D55AB3E5B4058CD77B3B03141401C5538E2A63E5B40707D4BB2AE314140B8FBE99AA43E5B40E0A834B3AD31414020C74CF89E3E5B40287D3736AB314140005A85FD993E5B407086E0FAA831414078138F6D943E5B40A0E46C79A6314140C8F51EEA7B3E5B40B86898AC9A31414000939578633E5B40B084C7818E31414054EC430C4B3E5B4078B0C02282314140E4D8E6D8973D5B40E078570D27314140B8F188538B3D5B40B826C3AF20314140E4D59BE6573D5B40F0D9508A0631414008125259223D5B40C805464FEB304140C026CFB5223D5B4098E28458E9304140944965DB203D5B40F0915446E63041401C6EAC03213D5B40888E056EE53041401477C5F11B3D5B40B87F7DF8E23041404033840D163D5B40B84F6238E0304140C4F4A7A10E3D5B40981CA180DC3041405CC6E6E0E23C5B40509F6AB5C63041406CE22299E33C5B40989B5FCFC2304140DC5DFE15823C5B4090C5D43591304140481640636C3C5B4058B1002C86304140ECC25FCB5F3C5B40384D77C27E3041405C2489D25C3C5B40E8EBAD797C3041400CDC85A9573C5B40D86FDE8678304140209423A9543C5B409874EB3A763041404C3F30334B3C5B4030C7ACFD6E304140082BADDD1B3C5B40A80504E74A304140B0FAB8DD1B3C5B4040F147E44A304140005CCC461C3C5B40C830B780323041408C7CCA431C3C5B4078332E7F32304140D4F3DF37C93B5B4000CE810E08304140D454D4D5873B5B4018A837A3E62F414054FD77D5873B5B4040782CA4E62F414038CADA767F3B5B40B8CDE8A2FC2F41405019D3747F3B5B40D8115BA2FC2F414018C611C9423B5B406042AC3BEC2F4140D8462BAD353B5B40A0B77D9EE52F414028B6E1CE2A3B5B40D8C33315E02F41402C36E474B93A5B4098CD3854A62F414084EE126FB93A5B4008984051A62F41401886034CB93A5B40502C56F8A62F4140E00872EDB73A5B409010ABCDAC2F41400426F5DAB63A5B4038267985B02F4140B0CDC248B63A5B4080BFB080B22F4140406C5ADDB53A5B40B80BE5C8B32F4140D41CE5A2B23A5B4098477B4CBC2F4140A4DE03A9B13A5B40A8F4F175BE2F414050217BB0B13A5B4010C4BE79BE2F414048E75014353B5B40E0D5146301304140945AFB17353B5B4068B9F364013041401C6300AB3A3B5B40B0EAB53E04304140C8F24A063E3B5B40808D21F605304140F480D136753B5B40F836EB6330304140E8E72433753B5B4018736CE44A304140E8E72433753B5B4068DABEE74A3041408C7DD7A7093C5B40F06BCBBD9630414074F7F31F123C5B40003EECB082304140BC052F20123C5B4020825EB082304140A035667C193C5B4010E22AB984304140C0F4103C473C5B40F037035E91304140F46E3298493C5B40E89DAF6B91304140FC761680503C5B40E0ABAC939130414060259FF1593C5B40C0C8B86D953041406446C878683C5B4080E5515C9B304140FCD12CADDF3C5B40C8CFB0FFD73041405C189C6DE03C5B40B020E0F2D3304140B4446397EF3C5B4000A6FCA6DB30414038F53BC6F53C5B40E0D056F0DE3041406838E85CFB3C5B4048A1596AE1304140788DE801003D5B404868A961E43041408C920A0F0C3D5B4058519A72EA30414024435201133D5B401015A54FEE30414064A2A434193D5B40D036C9BFF13041401484FC1D1E3D5B404094F529F4304140B4B94A471E3D5B4010A3443EF4304140C8809E661E3D5B4060323891F3304140580606F7203D5B40389CD2C4F230414010FC1322213D5B40B88F21DCF1304140E823A667213D5B4060322564F0304140C4256071213D5B4090101669F0304140C8551FC18D3D5B406015FB7A273141402C2369629A3D5B40909EADE62D314140088A6236C43D5B401866E929433141401416E537C43D5B40C065EA2243314140CC5C931ACC3D5B40B8EC152547314140849F9F19CC3D5B40380E512C473141400832B551CF3D5B40088131CF4831414064B4C252CF3D5B40885FF6C748314140B424C083DB3D5B4008E461FA4E31414078898582DB3D5B40F05D76014F3141409CF86EB6233E5B4050A6B6B3733141402892951D4A3E5B409028A437873141408C182889623E5B40A098239B933141401C6566017B3E5B40E856F2C59F314140DCD20B8D933E5B40202EC195AB3141402CE943129B3E5B40905AEE5AAF314140882173B19E3E5B4048BAE427B13141400809A3BCA33E5B40A8FE64A0B331414068EBD2ECA53E5B40E0B364B1B4314140C0A6E9AEAA3E5B406094DAD7B6314140F8FB063FB43E5B40083DEC09BB3141407C9C594DC23E5B40903C6710C1314140FCA3DEDCCA3E5B40C8ECDB9FC431414034C0E0C8D33E5B40A07D3941C8314140DCF37EA1DC3E5B4060D0BAC7CB314140BC47AC1BE83E5B4088626E3BD03141401C2B71EAF13E5B40B08AA9F4D331414050B58B73F83E5B40B054C15CD6314140A04BA446FE3E5B407071C378D83141408452A346173F5B40B891E134E1314140DC85CE54303F5B40806D0C8DE9314140244DF765493F5B40881883AFF131414050289D78493F5B40D845E80CF131414064F41F164D405B4080276FE544324140ACA734074D405B4060843C6445324140F4C163264E405B403886B8C04532414088BDAC306D405B4068F581C34F324140FCCCF4736C405B40606D09894C3241400CCF14D26B405B40189E7E5A49324140308767204E405B40A0582CBA3F324140");
        test.setCc("MULTIPOLYGON(((109.004768468 34.392569801, 109.004742859 34.392565533, 109.00474197 34.392568022, 108.988896666 34.390009251, 108.988898445 34.3900023150001, 108.987369187 34.3897542460001, 108.985841708 34.3894999500001, 108.984316719 34.3892337390001, 108.983874249 34.3891533600001, 108.983511809 34.3890859630001, 108.982633272 34.388918802, 108.982083388 34.3888108580001, 108.981597701 34.3887134080001, 108.980875844 34.388564919, 108.980168213 34.3884148290001, 108.97972272 34.3883182660001, 108.979207335 34.388204988, 108.978935771 34.3881438130001, 108.9787967 34.388113404, 108.978452754 34.388037469, 108.978148823 34.3879693600001, 108.977809324 34.3878928930001, 108.976313143 34.3875327820001, 108.97482123 34.3871614670001, 108.973330561 34.3867839280001, 108.962393022 34.384004276, 108.961628803 34.3838100150001, 108.95849004 34.383012094, 108.955221491 34.3821810810001, 108.955243542 34.3821211480001, 108.955130433 34.382027427, 108.955140036 34.382001641, 108.954830592 34.381926595, 108.954470996 34.381842659, 108.954018034 34.381729201, 108.951347566 34.381064107, 108.951391491 34.380945131, 108.945439814 34.379431466, 108.94411546 34.3790946010001, 108.943346828 34.3788683970001, 108.94316543 34.378798685, 108.942850476 34.378678187, 108.942667279 34.378608098, 108.942089841 34.3783871740001, 108.939200801 34.3772858400001, 108.939200812 34.3772855140001, 108.939225864 34.3765412230001, 108.939225147 34.3765410400001, 108.934156388 34.3752458700001, 108.930165727 34.3742260000001, 108.930165641 34.3742261140001, 108.929654802 34.37489735, 108.929654318 34.374897284, 108.925951259 34.3743967620001, 108.92515115 34.374194919, 108.924487801 34.374025965, 108.917569373 34.3722634580001, 108.917567986 34.3722631040001, 108.917559627 34.372283022, 108.917476045 34.3724610410001, 108.917410602 34.372574505, 108.917375746 34.37263497, 108.917350138 34.372674095, 108.917153095 34.3729339220001, 108.917093519 34.3729999, 108.917095299 34.3730003530001, 108.925114707 34.3750423290001, 108.925115581 34.375042552, 108.925455809 34.375129546, 108.92566068 34.3751819290001, 108.929029183 34.3764767550001, 108.929028307 34.377285531, 108.929028307 34.3772859270001, 108.938089333 34.3796002620001, 108.938606251 34.3789883760001, 108.938606306 34.3789883100001, 108.939055538 34.379050394, 108.941847817 34.3794362560001, 108.941991853 34.3794378860001, 108.942413351 34.3794426530001, 108.942989736 34.379560199, 108.943876453 34.37974123, 108.951152128 34.38159176, 108.951198008 34.381468162, 108.952123496 34.381703256, 108.952500876 34.3818035530001, 108.952841975 34.381879133, 108.953125455 34.38196965, 108.953861008 34.3821547750001, 108.954284983 34.382272678, 108.95466343 34.3823775990001, 108.954963204 34.3824512910001, 108.954973052 34.382453712, 108.954980521 34.382433083, 108.95513702 34.382408717, 108.955147285 34.3823809780001, 108.955163872 34.3823361570001, 108.955166191 34.3823367460001, 108.961777001 34.3840173460001, 108.962547877 34.38421329, 108.965100857 34.384862174, 108.965101217 34.38486134, 108.965582508 34.3849836690001, 108.965582281 34.384984531, 108.965778758 34.385034465, 108.965779009 34.3850336030001, 108.966523111 34.3852227190001, 108.966522818 34.3852235630001, 108.970929726 34.3863434450001, 108.973273655 34.386939006, 108.974764146 34.387317078, 108.976257658 34.387688392, 108.977755796 34.388048858, 108.978214804 34.3881639160001, 108.978435862 34.388218867, 108.978743705 34.388294267, 108.978877264 34.3883268110001, 108.979167679 34.3883924310001, 108.979751355 34.3885204700001, 108.980609262 34.388704348, 108.981131761 34.3888130020001, 108.981676311 34.388923791, 108.982216238 34.389031378, 108.982916754 34.3891672410001, 108.983515368 34.3892808750001, 108.983914267 34.3893543190001, 108.984269772 34.389418693, 108.98579565 34.389685259, 108.987324907 34.3899399100001, 108.988854877 34.390188159, 108.988859323 34.3901687750001, 109.004704982 34.3927275460001, 109.004701425 34.392742662, 109.004769895 34.392753687, 109.006664437 34.393059195, 109.006619443 34.3929606720001, 109.006580849 34.3928635710001, 109.004768468 34.392569801)))");
        String geojson = singleObject2Geojson(test, "cc");

        List<Test> aaa = new ArrayList<>();
        aaa.add(test);
        String geojson1 = listObject2Geojson(aaa, "cc");
        System.out.println(geojson);
        System.out.println(geojson1);
    }
}

 

标签:实体类,java,String,JSONObject,object,geojson,geomFildName,properties
From: https://www.cnblogs.com/z-double/p/17979356

相关文章

  • Java并发基础:Executor接口和Executors类的区别
    Executor是Java中的一个接口,它定义了一种将任务提交与任务执行机制(包括线程管理、调度等)分离的方式,Executors是一个工具类,它提供了多个静态工厂方法,用于创建不同类型的Executor实例。代码案例下面展示了如何使用Executor接口来执行异步任务,如下代码案例:importjava.util.conc......
  • [转]Java Stream API入门篇
    原文地址:JavaStreamAPI入门篇-CarpenterLee-博客园本文github地址你可能没意识到Java对函数式编程的重视程度,看看Java8加入函数式编程扩充多少功能就清楚了。Java8之所以费这么大功夫引入函数式编程,原因有二:代码简洁,函数式编程写出的代码简洁且意图明确,使用stream接口......
  • [转]Java Stream API进阶篇
    原文地址:JavaStreamAPI进阶篇-CarpenterLee-博客园本文github地址上一节介绍了部分Stream常见接口方法,理解起来并不困难,但Stream的用法不止于此,本节我们将仍然以Stream为例,介绍流的规约操作。规约操作(reductionoperation)又被称作折叠操作(fold),是通过某个连接动作将所有......
  • 一文搞清楚Java中的方法、常量、变量、参数
    写在开头在上一篇文章:一文搞清楚Java中的包、类、接口中我们讲了Java中的包、类和接口,今天继续将剩下的方法、常量、变量以及参数梳理完。Java中的变量与常量在JVM的运转中,承载的是数据,而数据的一种变现形式就是“量”,量分为:常量与变量,我们在数学和物理学中已经接触过变量的概......
  • Java 缓存池
    使用==对比的时候要注意,区分是int、Integer、newInteger。若是和int对比,不管是常量池还是堆的都会拆分成int,得到为true。若是和Integer对比,则要注意是否在缓存的范围之内即可。在java7之后,调用string.intern会判断字符串对象是否存在于缓存池,如果存在直接返回缓存池结果,如......
  • Java学习日记 Day6 JavaSE完结,步入正轨!!
    JavaSE:--------多线程--------①线程:线程在java中也是对象,也要创造线程类。②创建线程的三种方式:继承Thread:Thread是Runnable的实现类,而线程类进一步继承Thread类。实现Runnable:线程类直接对Runnable类进行实现。实现Callable:线程类实现Callable类。③线程的生命周期:线程从......
  • JAVA常用编码介绍
    ASCII码(最先采用的编码方式,专为英语使用)Unicode(为了克服ASCII码的缺点,方便其他国家使用,创造了Unicode意为统一编码的意思)UTF-8编码(为了克服Unicode编码的缺点:所有字符都用两个字节表示,浪费存储空间。为了节省空间又创造了UTF-8编码)......
  • Java基础语法
    Java基础语法基础阶段1、注释、标识符、关键字注释平时我们编写代码,在代码量比较少的时候,我们还可以看懂自己写的,但是当项目结构一旦复杂起来,我们就需要用到注释了。注释并不会被执行,是给我们写代码的人看的书写注释是一个非常好的习惯Java中的注释有三种:单行......
  • JAVA数据类型
    所有数据类型基本数据类型间的转换->自动类型转换->强制类型转换->基本数据类型间转换练习题基本数据类型和String类型间的转换->基本数据类型转为String类型->String数据类型转为基本数据类型......
  • 关于Java 数组
    了解Java数组Java中的数组是一种强大而灵活的数据结构,让我们一起深入探讨它的方方面面,从基础的概念到高级的应用。1.数组的创建与初始化首先,我们来看如何创建和初始化一个简单的整型数组:publicclassArrayExample{publicstaticvoidmain(String[]args){......