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

org.apache.baremaps.openstreetmap.function.WayGeometryBuilder Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you 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.
 */

package org.apache.baremaps.openstreetmap.function;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.baremaps.openstreetmap.model.Entity;
import org.apache.baremaps.openstreetmap.model.Way;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.geom.util.GeometryFixer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A consumer that builds and sets a way geometry via side effects.
 */
public class WayGeometryBuilder implements Consumer {

  private static final Logger logger = LoggerFactory.getLogger(WayGeometryBuilder.class);

  private final GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);

  private final Map coordinateMap;

  /**
   * Constructs a way geometry builder.
   *
   * @param coordinateMap the coordinates map
   */
  public WayGeometryBuilder(Map coordinateMap) {
    this.coordinateMap = coordinateMap;
  }

  /** {@inheritDoc} */
  @Override
  @SuppressWarnings("squid:S3776")
  public void accept(Entity entity) {
    if (entity instanceof Way way) {
      try {
        // Build the coordinate list and remove duplicates.
        List list = new ArrayList<>();
        Coordinate previous = null;
        for (Long id : way.getNodes()) {
          Coordinate coordinate = coordinateMap.get(id);
          if (coordinate != null && !coordinate.equals(previous)) {
            list.add(coordinate);
            previous = coordinate;
          }
        }

        Coordinate[] array = list.toArray(new Coordinate[0]);
        LineString line = geometryFactory.createLineString(array);

        if (!line.isEmpty()) {
          // Ways can be open or closed depending on the geometry or the tags:
          // https://wiki.openstreetmap.org/wiki/Way
          if (!line.isClosed()
              || way.getTags().containsKey("railway")
              || way.getTags().containsKey("highway")
              || way.getTags().containsKey("barrier")) {
            way.setGeometry(line);
          } else {
            Polygon polygon = geometryFactory.createPolygon(line.getCoordinates());
            if (polygon.isValid()) {
              way.setGeometry(polygon);
            } else {
              var geometryFixer = new GeometryFixer(polygon);
              var fixedGeometry = geometryFixer.getResult();
              way.setGeometry(fixedGeometry);
            }
          }
        }
      } catch (Exception e) {
        logger.debug("Unable to build the geometry for way #" + way.getId(), e);
        way.setGeometry(geometryFactory.createEmpty(0));
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy