Geometries as GeoJson

99 views
Skip to first unread message

Jon Inazio Sanchez Martinez

unread,
Jan 23, 2018, 1:35:09 PM1/23/18
to modelmapper
I just found ModelMapper and I've found it very useful to generate DTOs.

Anyway, when it comes to serialize objects which own a JTS Geometry data type attribute, I'd want them to be in GeoJson format.

For the time being, the following class...

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.vividsolutions.jts.geom.MultiPolygon;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "municipios")
@Builder
@Data
@EqualsAndHashCode
@NoArgsConstructor
@AllArgsConstructor
public class Municipio implements Serializable {

/*
* Atributos
*/
@Transient
private static final long serialVersionUID = -2486158354849124667L;

@Id
private Integer gid;
private String codigo;
private String texto;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
            name = "cod_prov", 
            referencedColumnName = "codigo"
        )
private Provincia provincia;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(
            name = "cod_ccaa", 
            referencedColumnName = "codigo"
        )
private ComunidadAutonoma comunidad;
}


...gets serialized as this...

"municipio": {
        "gid": 27,
        "codigo": "01036",
        "texto": "Laudio/Llodio",
        "provincia": {
            "gid": 1,
            "codigo": "01",
            "texto": "Álava",
            "texto_alt": "Araba",
            "comunidad": {
                "gid": 16,
                "codigo": "16",
                "texto": "País Vasco",
                "texto_alt": "Euskadi"
            }
        },
        "geom": "MULTIPOLYGON (((-2.9420491414616916 43.14380029306756, -2.9408165999002556 43.14430841999599, -2.939059950482368 43.141709610211024, -2.94050966386134 43.13799144046195, -2.936693413950487 43.13417138986535, -2.942182362664065 43.130752517734585, -2.943746497171919 43.126998320661244, -2.9491831049375983 43.122917261507745, -2.9568712631986553 43.11281715256084, -2.958883438671909 43.11305654097181, -2.9659615390025005 43.103108652799214, -2.9652067663290955 43.10264468267702, -2.9762492404051137 43.097150217253514, -2.9899968551814102 43.1013215781132, -2.9963369436287586 43.10706705889331, -3.0097035041930043 43.11278484125608, -3.0129149314121726 43.116877214910765, -3.017330715593204 43.116457973261085, -3.019790265011745 43.11919501110562, -3.02040901781778 43.12611504150391, -3.02393972418118 43.12847813412591, -3.025215180146138 43.13126035313113, -3.021643236846659 43.137154623532716, -3.013836762498057 43.1408026665335, -3.005568207272493 43.148466258438624, -3.0026147541242167 43.14959643860667, -2.9909328393119017 43.1625087272497, -2.9796315563151357 43.17050333013982, -2.9766664577130144 43.17485199744999, -2.9736646499823842 43.17600841462698, -2.979262791265818 43.18166902950952, -2.9742758156428035 43.18436486223879, -2.97110139733367 43.18276575229797, -2.969942290790593 43.179992017776314, -2.961893498167311 43.17338464369854, -2.957182021700495 43.17193318237574, -2.955167271064855 43.170041426431084, -2.9475444948015594 43.16848931161088, -2.955018585037522 43.157632918409604, -2.9474297891472103 43.151515433493785, -2.9420491414616916 43.14380029306756)))"
    }

I've found the following artifacts, but I don't know how to configure Model Mapper to make use of either:

<dependency>
    <groupId>org.modelmapper.extensions</groupId>
    <artifactId>modelmapper-jackson</artifactId>
    <version>${modelmapper-jackson.version}</version>
</dependency>

<dependency>
    <groupId>com.bedatadriven</groupId>
    <artifactId>jackson-datatype-jts</artifactId>
    <version>${jackson-datatype-jts.version}</version>
</dependency> 

Any hint? Thank you!

Chun Han Hsiao

unread,
Apr 8, 2018, 9:42:52 PM4/8/18
to modelmapper
Hi,

First, you need to create a Dto class that have structure followed the spec of GeoJson (or using https://mvnrepository.com/artifact/com.vividsolutions/jts/1.13 that already provide the GeoJson classes).
Then you can using modelmapper to map the Entity to the Dto of GeoJson. (Please check http://modelmapper.org/user-manual/property-mapping/)
After that, you can use the Jackson to serialize the Dto to json formatted text.


Jon Inazio Sanchez Martinez於 2018年1月24日星期三 UTC+8上午2時35分09秒寫道:
Reply all
Reply to author
Forward
0 new messages