All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.geolatte.geom.jts.PointSequenceCoordinateSequenceFactory Maven / Gradle / Ivy

Go to download

This geoLatte-geom library offers a geometry model that conforms to the OGC Simple Features for SQL specification.

There is a newer version: 1.9.1
Show newest version
/*
 * This file is part of the GeoLatte project.
 *
 *     GeoLatte is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     GeoLatte is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with GeoLatte.  If not, see .
 *
 * Copyright (C) 2010 - 2011 and Ownership of code is shared by:
 * Qmino bvba - Romeinsestraat 18 - 3001 Heverlee  (http://www.qmino.com)
 * Geovise bvba - Generaal Eisenhowerlei 9 - 2140 Antwerpen (http://www.geovise.com)
 */

package org.geolatte.geom.jts;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.CoordinateSequence;
import com.vividsolutions.jts.geom.CoordinateSequenceFactory;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import org.geolatte.geom.*;
import org.geolatte.geom.crs.CoordinateReferenceSystem;
import org.geolatte.geom.crs.CoordinateSystem;

import java.util.Arrays;

import static org.geolatte.geom.crs.CoordinateReferenceSystems.*;

/**
 * A CoordinateSequenceFactory that creates PointSequences (which extend
 * CoordinateSequences).
 *
 * @author Karel Maesen, Geovise BVBA
 *         creation-date: 11/22/11
 */
class PointSequenceCoordinateSequenceFactory implements CoordinateSequenceFactory {

    @Override
    public CoordinateSequence create(Coordinate[] coordinates) {
        CoordinateReferenceSystem crs = determineCRS(coordinates);
        return fromCoordinateArray(coordinates, crs);
    }

    @Override
    public CoordinateSequence create(CoordinateSequence coordSeq) {
        return coordSeq;
    }

    @Override
    public CoordinateSequence create(int size, int dimension) {
        // This is causing problems when working with Geotools JTS class in
        // transformation.
        // This is just a quick fix in order to get this working...
        if (dimension > 3)
            throw new IllegalArgumentException("dimension must be <= 3");
        return new CoordinateArraySequence(size, dimension);
    }

    @SuppressWarnings("unchecked")
    public 

PositionSequence

toPositionSequence(CoordinateSequence cs, Class

posType, CoordinateReferenceSystem

crs) { if (cs instanceof PositionSequence && ((PositionSequence) cs).getPositionClass().equals(posType)) { return (PositionSequence

) cs; } Coordinate c = new Coordinate(); double[] psc = new double[crs.getCoordinateDimension()]; Arrays.fill(psc, Double.NaN); PositionSequenceBuilder

builder = PositionSequenceBuilders.fixedSized(cs.size(), posType); for (int i = 0; i < cs.size(); i++) { psc[0] = cs.getOrdinate(i, 0); psc[1] = cs.getOrdinate(i, 1); if (hasVerticalAxis(crs)) { psc[2] = cs.getOrdinate(i, 2); } builder.add(psc); } return builder.toPositionSequence(); } private CoordinateReferenceSystem determineCRS(Coordinate[] coordinates) { boolean hasZ, hasM = false; if (coordinates == null || coordinates.length == 0) return PROJECTED_2D_METER; if (coordinates[0] instanceof DimensionalCoordinate) { hasM = !Double.isNaN(((DimensionalCoordinate) coordinates[0]).getM()); } hasZ = !Double.isNaN(coordinates[0].z); if (hasM && hasZ) { return PROJECTED_3DM_METER; } else if (hasM) { return PROJECTED_2DM_METER; } else if (hasZ) { return PROJECTED_3D_METER; } else { return PROJECTED_2D_METER; } } private

CoordinateSequence fromCoordinateArray(Coordinate[] coordinates, CoordinateReferenceSystem

crs) { PositionSequenceBuilder

builder = PositionSequenceBuilders.fixedSized(coordinates.length, crs.getPositionClass()); double[] ordinates = new double[crs.getCoordinateDimension()]; for (Coordinate co : coordinates) { copy(co, ordinates, crs); builder.add(ordinates); } return (CoordinateSequence) builder.toPositionSequence(); } private

void copy(Coordinate co, double[] ordinates, CoordinateReferenceSystem

crs) { ordinates[0] = co.x; ordinates[1] = co.y; boolean hasVerticalAxis = hasVerticalAxis(crs); if (hasVerticalAxis) { ordinates[2] = co.z; } if (hasMeasureAxis(crs)) { int idxM = hasVerticalAxis ? 3 : 2; ordinates[idxM] = (co instanceof DimensionalCoordinate) ? ((DimensionalCoordinate) co).m : Double.NaN; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy