Search in sources :

Example 86 with Point

use of com.vividsolutions.jts.geom.Point in project GeoGig by boundlessgeo.

the class ChangesetContentsScanner method parsePrimitive.

Primitive parsePrimitive(XMLStreamReader reader) throws XMLStreamException {
    reader.require(START_ELEMENT, null, null);
    final String primitiveName = reader.getLocalName();
    checkArgument(PRIMITIVE_TAGS.contains(primitiveName));
    Primitive primitive = inferrPrimitive(reader);
    primitive.setId(Long.valueOf(reader.getAttributeValue(null, "id")));
    primitive.setVisible(Boolean.valueOf(reader.getAttributeValue(null, "visible")));
    primitive.setTimestamp(parseDateTime(reader.getAttributeValue(null, "timestamp")));
    primitive.setUserName(reader.getAttributeValue(null, "user"));
    Long uid = Long.valueOf(fromNullable(reader.getAttributeValue(null, "uid")).or("-1"));
    primitive.setUserId(uid);
    Integer version = Integer.valueOf(fromNullable(reader.getAttributeValue(null, "version")).or("1"));
    primitive.setVersion(version);
    primitive.setChangesetId(Long.valueOf(reader.getAttributeValue(null, "changeset")));
    if (primitive instanceof Node) {
        Node node = (Node) primitive;
        String lat = reader.getAttributeValue(null, "lat");
        String lon = reader.getAttributeValue(null, "lon");
        // may be null in case of a delete change
        if (lat != null && lon != null) {
            double x = Double.valueOf(lon);
            double y = Double.valueOf(lat);
            Point location = GEOMFACT.createPoint(new Coordinate(x, y));
            node.setLocation(location);
        }
        parseNodeContents(node, reader);
    } else if (primitive instanceof Way) {
        Way way = (Way) primitive;
        parseWayContents(way, reader);
    } else {
        Relation relation = (Relation) primitive;
        parseRelationContents(relation, reader);
    }
    reader.require(END_ELEMENT, null, primitiveName);
    return primitive;
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) Point(com.vividsolutions.jts.geom.Point)

Example 87 with Point

use of com.vividsolutions.jts.geom.Point in project GeoGig by boundlessgeo.

the class GeometrySerializer method read.

@Override
public Geometry read(DataInput in) throws IOException {
    final int typeAndMasks = readUnsignedVarInt(in);
    Geometry geom;
    if ((typeAndMasks & POINT) == POINT) {
        geom = GEOMFAC.createPoint(EncodingSequenceFilter.readCoordinate(in));
    } else if ((typeAndMasks & LINESTRING) == LINESTRING) {
        CoordinateSequence cs = EncodingSequenceFilter.read(in);
        geom = GEOMFAC.createLineString(cs);
    } else {
        throw new UnsupportedOperationException();
    }
    return geom;
}
Also used : Geometry(com.vividsolutions.jts.geom.Geometry) CoordinateSequence(com.vividsolutions.jts.geom.CoordinateSequence) Point(com.vividsolutions.jts.geom.Point) MultiPoint(com.vividsolutions.jts.geom.MultiPoint)

Example 88 with Point

use of com.vividsolutions.jts.geom.Point in project GeoGig by boundlessgeo.

the class OSMHistoryImport method insertChanges.

/**
     * @param cli
     * @param changes
     * @param featureFilter
     * @throws IOException
     */
private long insertChanges(GeogigCLI cli, final Iterator<Change> changes, @Nullable Envelope featureFilter) throws IOException {
    final GeoGIG geogig = cli.getGeogig();
    final Repository repository = geogig.getRepository();
    final WorkingTree workTree = repository.workingTree();
    Map<Long, Coordinate> thisChangePointCache = new LinkedHashMap<Long, Coordinate>() {

        /** serialVersionUID */
        private static final long serialVersionUID = 1277795218777240552L;

        @Override
        protected boolean removeEldestEntry(Map.Entry<Long, Coordinate> eldest) {
            return size() == 10000;
        }
    };
    long cnt = 0;
    Set<String> deletes = Sets.newHashSet();
    Multimap<String, SimpleFeature> insertsByParent = HashMultimap.create();
    while (changes.hasNext()) {
        Change change = changes.next();
        final String featurePath = featurePath(change);
        if (featurePath == null) {
            // ignores relations
            continue;
        }
        final String parentPath = NodeRef.parentPath(featurePath);
        if (Change.Type.delete.equals(change.getType())) {
            cnt++;
            deletes.add(featurePath);
        } else {
            final Primitive primitive = change.getNode().isPresent() ? change.getNode().get() : change.getWay().get();
            final Geometry geom = parseGeometry(geogig, primitive, thisChangePointCache);
            if (geom instanceof Point) {
                thisChangePointCache.put(Long.valueOf(primitive.getId()), ((Point) geom).getCoordinate());
            }
            SimpleFeature feature = toFeature(primitive, geom);
            if (featureFilter == null || featureFilter.intersects((Envelope) feature.getBounds())) {
                insertsByParent.put(parentPath, feature);
                cnt++;
            }
        }
    }
    for (String parentPath : insertsByParent.keySet()) {
        Collection<SimpleFeature> features = insertsByParent.get(parentPath);
        if (features.isEmpty()) {
            continue;
        }
        Iterator<? extends Feature> iterator = features.iterator();
        ProgressListener listener = new DefaultProgressListener();
        List<org.locationtech.geogig.api.Node> insertedTarget = null;
        Integer collectionSize = Integer.valueOf(features.size());
        workTree.insert(parentPath, iterator, listener, insertedTarget, collectionSize);
    }
    if (!deletes.isEmpty()) {
        workTree.delete(deletes.iterator());
    }
    return cnt;
}
Also used : Node(org.locationtech.geogig.osm.internal.history.Node) Envelope(com.vividsolutions.jts.geom.Envelope) LinkedHashMap(java.util.LinkedHashMap) WorkingTree(org.locationtech.geogig.repository.WorkingTree) Entry(java.util.Map.Entry) Primitive(org.locationtech.geogig.osm.internal.history.Primitive) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) Change(org.locationtech.geogig.osm.internal.history.Change) Point(com.vividsolutions.jts.geom.Point) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Geometry(com.vividsolutions.jts.geom.Geometry) Repository(org.locationtech.geogig.repository.Repository) ProgressListener(org.locationtech.geogig.api.ProgressListener) DefaultProgressListener(org.locationtech.geogig.api.DefaultProgressListener) Coordinate(com.vividsolutions.jts.geom.Coordinate) GeoGIG(org.locationtech.geogig.api.GeoGIG)

Example 89 with Point

use of com.vividsolutions.jts.geom.Point in project GeoGig by boundlessgeo.

the class OSMHistoryImport method parseGeometry.

/**
     * @param primitive
     * @param thisChangePointCache
     * @return
     */
private Geometry parseGeometry(GeoGIG geogig, Primitive primitive, Map<Long, Coordinate> thisChangePointCache) {
    if (primitive instanceof Relation) {
        return null;
    }
    if (primitive instanceof Node) {
        Optional<Point> location = ((Node) primitive).getLocation();
        return location.orNull();
    }
    final Way way = (Way) primitive;
    final ImmutableList<Long> nodes = way.getNodes();
    StagingArea index = geogig.getRepository().index();
    FeatureBuilder featureBuilder = new FeatureBuilder(NODE_REV_TYPE);
    List<Coordinate> coordinates = Lists.newArrayList(nodes.size());
    FindTreeChild findTreeChild = geogig.command(FindTreeChild.class);
    findTreeChild.setIndex(true);
    ObjectId rootTreeId = geogig.command(ResolveTreeish.class).setTreeish(Ref.HEAD).call().get();
    if (!rootTreeId.isNull()) {
        RevTree headTree = geogig.command(RevObjectParse.class).setObjectId(rootTreeId).call(RevTree.class).get();
        findTreeChild.setParent(headTree);
    }
    for (Long nodeId : nodes) {
        Coordinate coord = thisChangePointCache.get(nodeId);
        if (coord == null) {
            String fid = String.valueOf(nodeId);
            String path = NodeRef.appendChild(NODE_TYPE_NAME, fid);
            Optional<org.locationtech.geogig.api.Node> ref = index.findStaged(path);
            if (!ref.isPresent()) {
                Optional<NodeRef> nodeRef = findTreeChild.setChildPath(path).call();
                if (nodeRef.isPresent()) {
                    ref = Optional.of(nodeRef.get().getNode());
                } else {
                    ref = Optional.absent();
                }
            }
            if (ref.isPresent()) {
                org.locationtech.geogig.api.Node nodeRef = ref.get();
                RevFeature revFeature = index.getDatabase().getFeature(nodeRef.getObjectId());
                String id = NodeRef.nodeFromPath(nodeRef.getName());
                Feature feature = featureBuilder.build(id, revFeature);
                Point p = (Point) ((SimpleFeature) feature).getAttribute("location");
                if (p != null) {
                    coord = p.getCoordinate();
                    thisChangePointCache.put(Long.valueOf(nodeId), coord);
                }
            }
        }
        if (coord != null) {
            coordinates.add(coord);
        }
    }
    if (coordinates.size() < 2) {
        return null;
    }
    return GEOMF.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
}
Also used : SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) FeatureBuilder(org.locationtech.geogig.api.FeatureBuilder) Node(org.locationtech.geogig.osm.internal.history.Node) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Feature(org.opengis.feature.Feature) RevFeature(org.locationtech.geogig.api.RevFeature) Way(org.locationtech.geogig.osm.internal.history.Way) NodeRef(org.locationtech.geogig.api.NodeRef) Relation(javax.management.relation.Relation) ResolveTreeish(org.locationtech.geogig.api.plumbing.ResolveTreeish) RevFeature(org.locationtech.geogig.api.RevFeature) ObjectId(org.locationtech.geogig.api.ObjectId) StagingArea(org.locationtech.geogig.repository.StagingArea) FindTreeChild(org.locationtech.geogig.api.plumbing.FindTreeChild) Point(com.vividsolutions.jts.geom.Point) Coordinate(com.vividsolutions.jts.geom.Coordinate) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) RevTree(org.locationtech.geogig.api.RevTree)

Example 90 with Point

use of com.vividsolutions.jts.geom.Point in project GeoGig by boundlessgeo.

the class EntityConverter method toEntity.

/**
     * Converts a Feature to a OSM Entity
     * 
     * @param feature the feature to convert
     * @param replaceId. The changesetId to use in case the feature has a negative one indicating a
     *        temporary value
     * @return
     */
public Entity toEntity(SimpleFeature feature, Long changesetId) {
    Entity entity;
    SimpleFeatureType type = feature.getFeatureType();
    long id = Long.parseLong(feature.getID());
    int version = ((Integer) feature.getAttribute("version")).intValue();
    Long changeset = (Long) feature.getAttribute("changeset");
    if (changesetId != null && changeset < 0) {
        changeset = changesetId;
    }
    Long milis = (Long) feature.getAttribute("timestamp");
    Date timestamp = new Date(milis);
    String user = (String) feature.getAttribute("user");
    String[] userTokens = user.split(":");
    OsmUser osmuser;
    try {
        osmuser = new OsmUser(Integer.parseInt(userTokens[1]), userTokens[0]);
    } catch (Exception e) {
        osmuser = OsmUser.NONE;
    }
    String tagsString = (String) feature.getAttribute("tags");
    Collection<Tag> tags = OSMUtils.buildTagsCollectionFromString(tagsString);
    CommonEntityData entityData = new CommonEntityData(id, version, timestamp, osmuser, changeset, tags);
    if (type.equals(OSMUtils.nodeType())) {
        Point pt = (Point) feature.getDefaultGeometryProperty().getValue();
        entity = new Node(entityData, pt.getY(), pt.getX());
    } else {
        List<WayNode> nodes = Lists.newArrayList();
        String nodesString = (String) feature.getAttribute("nodes");
        for (String s : nodesString.split(";")) {
            nodes.add(new WayNode(Long.parseLong(s)));
        }
        entity = new Way(entityData, nodes);
    }
    return entity;
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) CommonEntityData(org.openstreetmap.osmosis.core.domain.v0_6.CommonEntityData) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) OsmUser(org.openstreetmap.osmosis.core.domain.v0_6.OsmUser) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) Point(com.vividsolutions.jts.geom.Point) Point(com.vividsolutions.jts.geom.Point) Date(java.util.Date) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Tag(org.openstreetmap.osmosis.core.domain.v0_6.Tag)

Aggregations

Point (com.vividsolutions.jts.geom.Point)157 Geometry (com.vividsolutions.jts.geom.Geometry)53 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)49 Coordinate (com.vividsolutions.jts.geom.Coordinate)46 ArrayList (java.util.ArrayList)27 Polygon (com.vividsolutions.jts.geom.Polygon)26 LineString (com.vividsolutions.jts.geom.LineString)25 SamplePoint (org.datanucleus.samples.jtsgeometry.SamplePoint)22 List (java.util.List)20 PersistenceManager (javax.jdo.PersistenceManager)19 Transaction (javax.jdo.Transaction)19 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)18 Query (javax.jdo.Query)17 Test (org.junit.Test)17 MultiPolygon (com.vividsolutions.jts.geom.MultiPolygon)15 HashMap (java.util.HashMap)15 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)12 QuadPointDouble (net.osmand.data.QuadPointDouble)12 GeometryCollection (com.vividsolutions.jts.geom.GeometryCollection)10 LinearRing (com.vividsolutions.jts.geom.LinearRing)10