Commit 9d602c43 authored by Noel Alonso's avatar Noel Alonso
Browse files

Merge branch 'feature-polygonSupport' into 'dev'

Añade soporte para geometría tipo polygon

See merge request redmic-project/server/library/jts4jackson!4
parents 1e1480e9 b04eafae
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -13,7 +13,7 @@
	<groupId>es.redmic.lib</groupId>
	<artifactId>jts4jackson</artifactId>
	<packaging>jar</packaging>
	<version>0.1.0</version>
	<version>0.1.0-feature-polygonSupport</version>
	<name>jts4jackson</name>
	<description>Jts serialization and deserialization for Jackson</description>

+5 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ package es.redmic.jts4jackson.module;

import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;

import com.fasterxml.jackson.core.json.PackageVersion;
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -30,6 +31,8 @@ import es.redmic.jts4jackson.linestring.LineStringDeserializer;
import es.redmic.jts4jackson.linestring.LineStringSerializer;
import es.redmic.jts4jackson.point.PointDeserializer;
import es.redmic.jts4jackson.point.PointSerializer;
import es.redmic.jts4jackson.polygon.PolygonDeserializer;
import es.redmic.jts4jackson.polygon.PolygonSerializer;

public class JTSModule extends SimpleModule {

@@ -42,10 +45,12 @@ public class JTSModule extends SimpleModule {
		// deserializers
		addDeserializer(Point.class, new PointDeserializer());
		addDeserializer(LineString.class, new LineStringDeserializer());
		addDeserializer(Polygon.class, new PolygonDeserializer());

		// serializers:
		addSerializer(Point.class, new PointSerializer());
		addSerializer(LineString.class, new LineStringSerializer());
		addSerializer(Polygon.class, new PolygonSerializer());
	}

	@Override
+50 −0
Original line number Diff line number Diff line
package es.redmic.jts4jackson.polygon;

/*-
 * #%L
 * jts4jackson
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.IOException;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.spatial4j.io.jackson.GeometryDeserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class PolygonDeserializer extends JsonDeserializer<Polygon> {

	final GeometryDeserializer dser;

	public PolygonDeserializer() {
		dser = new GeometryDeserializer();
	}

	@Override
	public Polygon deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		Geometry geometry = dser.deserialize(jp, ctxt);
		if (geometry != null) {
			return (Polygon) geometry;
		}
		return null;
	}
}
+44 −0
Original line number Diff line number Diff line
package es.redmic.jts4jackson.polygon;

/*-
 * #%L
 * jts4jackson
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.IOException;

import org.locationtech.jts.geom.Polygon;
import org.locationtech.spatial4j.io.jackson.GeometryAsGeoJSONSerializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class PolygonSerializer extends JsonSerializer<Polygon> {

	final GeometryAsGeoJSONSerializer ser;

	public PolygonSerializer() {
		ser = new GeometryAsGeoJSONSerializer();
	}

	@Override
	public void serialize(Polygon value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		ser.serialize(value, gen, serializers);
	}
}
+74 −0
Original line number Diff line number Diff line
package es.redmic.jts4jackson.polygon;

/*-
 * #%L
 * jts4jackson
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.IOException;
import java.nio.file.Files;

import org.json.JSONException;
import org.junit.Test;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.skyscreamer.jsonassert.JSONAssert;
import org.springframework.core.io.ClassPathResource;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;

import es.redmic.jts4jackson.module.JTSModule;

public class PolygonSerializerDeserializerTest {

	private String dataFile = "/data/polygon.json";

	ObjectMapper mapper = new ObjectMapper();

	@Test
	public void sourceAndExpectedJson_AreEqual_IfDeserializeAndSerializePolygon()
			throws JsonParseException, JsonMappingException, IOException, JSONException {

		mapper.registerModule(new JTSModule());

		Polygon polygon = mapper.readValue(getClass().getResource(dataFile).openStream(), Polygon.class);

		String source = new String(Files.readAllBytes(new ClassPathResource(dataFile).getFile().toPath()));

		String expected = mapper.writeValueAsString(polygon);

		JSONAssert.assertEquals(expected, source, false);
	}

	@Test(expected = InvalidDefinitionException.class)
	public void deserialize_ThrowExeption_IfModuleIsNotRegistry()
			throws JsonParseException, JsonMappingException, IOException, JSONException {

		mapper.readValue(getClass().getResource(dataFile).openStream(), Polygon.class);
	}

	@Test(expected = JsonMappingException.class)
	public void serialize_ThrowExeption_IfModuleIsNotRegistry()
			throws JsonParseException, JsonMappingException, IOException, JSONException {

		mapper.writeValueAsString(new GeometryFactory().createPolygon());
	}
}
Loading