Search in sources :

Example 1 with AbstractGeometry

use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.

the class KmlFeatureUtilities method buildKMLFeature.

/**
 * create a {@link Geometry from a Feature.}
 *
 * @param noKmlFeature feature from an other type
 * @param defaultIdStyle style defined on document.
 * @return a valid kml {@link Feature}
 */
public static Feature buildKMLFeature(Feature noKmlFeature, IdAttributes defaultIdStyle) {
    // Transform geometry
    final KmlFactory kmlFactory = DefaultKmlFactory.getInstance();
    final Feature placemark = kmlFactory.createPlacemark();
    final String geoColumn = AttributeConvention.GEOMETRY;
    final AbstractGeometry ag = buildKMLGeometry((Geometry) noKmlFeature.getPropertyValue(geoColumn));
    placemark.setPropertyValue(KmlConstants.TAG_GEOMETRY, ag);
    try {
        placemark.setPropertyValue(KmlConstants.TAG_STYLE_URL, new URI("#" + defaultIdStyle.getId()));
    } catch (URISyntaxException e) {
        LOGGER.log(Level.WARNING, "unnable to define style URI", e);
    }
    // TODO : transform datas
    final List<Data> simpleDatas = new ArrayList<>(0);
    for (final PropertyType type : noKmlFeature.getType().getProperties(true)) {
        final Property property = noKmlFeature.getProperty(type.getName().toString());
        String localPartName = property.getName().tip().toString();
        final Object value = property.getValue();
        if (localPartName.equalsIgnoreCase(KmlConstants.TAG_NAME)) {
            placemark.setPropertyValue(KmlConstants.TAG_NAME, value);
        } else if (!(property.getName().toString().equalsIgnoreCase(geoColumn) || localPartName.equalsIgnoreCase("fid"))) {
            if (value != null) {
                Data simpleData = kmlFactory.createData();
                simpleData.setName(localPartName);
                simpleData.setValue(value.toString());
                simpleDatas.add(simpleData);
            }
        }
    }
    if (!simpleDatas.isEmpty()) {
        ExtendedData extendedData = kmlFactory.createExtendedData();
        extendedData.setDatas(simpleDatas);
        placemark.setPropertyValue(KmlConstants.TAG_EXTENDED_DATA, extendedData);
    }
    return placemark;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) ArrayList(java.util.ArrayList) SimpleData(org.geotoolkit.data.kml.model.SimpleData) ExtendedData(org.geotoolkit.data.kml.model.ExtendedData) SchemaData(org.geotoolkit.data.kml.model.SchemaData) DefaultExtendedData(org.geotoolkit.data.kml.model.DefaultExtendedData) Data(org.geotoolkit.data.kml.model.Data) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) URISyntaxException(java.net.URISyntaxException) PropertyType(org.opengis.feature.PropertyType) Feature(org.opengis.feature.Feature) URI(java.net.URI) Property(org.opengis.feature.Property)

Example 2 with AbstractGeometry

use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.

the class KmlFeatureUtilities method extractFeature.

/**
 * create a {@link SimpleFeature} from an {@link Map.Entry}
 * @param idgeom current id iterator
 * @param geometry : {@link Map.Entry} which contains a geometry
 * @return a {@link SimpleFeature}
 */
private static Feature extractFeature(int idgeom, Map.Entry<Object, Map<String, String>> geometry) {
    final Object geom = geometry.getKey();
    Geometry finalGeom = null;
    // if it's a simple geometry
    if (geom instanceof Geometry) {
        finalGeom = (Geometry) geometry.getKey();
        try {
            // if it's lineString it can cut meridian. So test it
            if (finalGeom instanceof LineString) {
                LineString lineString = (LineString) finalGeom;
                MultiLineString multiLine = cutAtMeridian(lineString);
                if (multiLine != null) {
                    finalGeom = multiLine;
                }
            }
        } catch (Exception ex) {
            LOGGER.log(Level.WARNING, ex.getMessage(), ex);
        }
    } else // it's a geometry collection
    if (geom instanceof DefaultMultiGeometry) {
        final DefaultMultiGeometry kmlabstractGeometry = (DefaultMultiGeometry) geom;
        final List<Geometry> multiGeometry = new ArrayList<>(0);
        // loop on geometry to add id on a GeometryList
        for (AbstractGeometry abstractGeometry : kmlabstractGeometry.getGeometries()) {
            if (abstractGeometry instanceof Geometry) {
                final Geometry currentGeom = (Geometry) abstractGeometry;
                multiGeometry.add(currentGeom);
            }
        }
        final GeometryFactory gf = JTS.getFactory();
        Geometry[] geometryArray = new Geometry[multiGeometry.size()];
        for (int i = 0; i < multiGeometry.size(); i++) {
            geometryArray[i] = multiGeometry.get(i);
        }
        finalGeom = new GeometryCollection(geometryArray, gf);
    }
    if (finalGeom != null) {
        return BuildSimpleFeature(idgeom, geometry.getValue(), finalGeom);
    }
    return null;
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) Geometry(org.locationtech.jts.geom.Geometry) MultiGeometry(org.geotoolkit.data.kml.model.MultiGeometry) GeometryCollection(org.locationtech.jts.geom.GeometryCollection) MultiLineString(org.locationtech.jts.geom.MultiLineString) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) MultiLineString(org.locationtech.jts.geom.MultiLineString) LineString(org.locationtech.jts.geom.LineString) DefaultMultiGeometry(org.geotoolkit.data.kml.model.DefaultMultiGeometry) List(java.util.List) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException)

Example 3 with AbstractGeometry

use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.

the class KMLGraphicBuilder method legendPlacemark.

private static Image legendPlacemark(Feature placemark, KmlCache cache) throws IOException {
    int nameWidth = 0;
    legendCommonFeature(placemark, cache);
    final String featureName = (String) placemark.getPropertyValue(KmlConstants.TAG_NAME);
    if (featureName != null) {
        nameWidth = FONT_METRICS.stringWidth(featureName);
    }
    // Apply styles
    final Style s = retrieveStyle(placemark, cache);
    final BufferedImage image = new BufferedImage(LEGEND_WIDTH_EXT + nameWidth, LEGEND_HEIGHT_EXT, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D graphic = (Graphics2D) image.getGraphics();
    graphic.setFont(FONT);
    final AbstractGeometry geometry = (AbstractGeometry) placemark.getPropertyValue(KmlConstants.TAG_GEOMETRY);
    if (s != null && s.getIconStyle() != null) {
        final IconStyle iconStyle = s.getIconStyle();
        final BasicLink bl = iconStyle.getIcon();
        if (bl != null) {
            if (bl.getHref() != null) {
                final URL img = new URL(bl.getHref());
                final BufferedImage buff = ImageIO.read(img);
                graphic.drawImage(buff, 0, 4, LEGEND_WIDTH_INT, LEGEND_HEIGHT_INT, null);
            }
        }
    } else if (geometry instanceof LineString) {
        graphic.drawImage(ICON_PLACEMARK_LINE_STRING, 0, 4, null);
    } else if (geometry instanceof LinearRing) {
        graphic.drawImage(ICON_PLACEMARK_LINEAR_RING, 0, 4, null);
    } else if (geometry instanceof Polygon) {
        graphic.drawImage(ICON_PLACEMARK_POLYGON, 0, 4, null);
    } else {
        graphic.drawImage(ICON_PLACEMARK, 0, 4, null);
    }
    if (featureName != null) {
        graphic.setColor(Color.BLACK);
        graphic.drawString(featureName, LEGEND_WIDTH_EXT, LEGEND_HEIGHT_INT);
    }
    return image;
}
Also used : IconStyle(org.geotoolkit.data.kml.model.IconStyle) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) LineString(org.geotoolkit.data.kml.model.LineString) LineStyle(org.geotoolkit.data.kml.model.LineStyle) IconStyle(org.geotoolkit.data.kml.model.IconStyle) BalloonStyle(org.geotoolkit.data.kml.model.BalloonStyle) Style(org.geotoolkit.data.kml.model.Style) PolyStyle(org.geotoolkit.data.kml.model.PolyStyle) LabelStyle(org.geotoolkit.data.kml.model.LabelStyle) LineString(org.geotoolkit.data.kml.model.LineString) LinearRing(org.geotoolkit.data.kml.model.LinearRing) Polygon(org.geotoolkit.data.kml.model.Polygon) Point(org.geotoolkit.data.kml.model.Point) BufferedImage(java.awt.image.BufferedImage) URL(java.net.URL) Graphics2D(java.awt.Graphics2D) BasicLink(org.geotoolkit.data.kml.model.BasicLink)

Example 4 with AbstractGeometry

use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.

the class KmlReader method readMultiGeometry.

private MultiGeometry readMultiGeometry() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractGeometry
    List<SimpleTypeContainer> abstractGeometrySimpleExtensions = new ArrayList<>();
    List<Object> abstractGeometryObjectExtensions = new ArrayList<>();
    // Multi Geometry
    List<AbstractGeometry> geometries = new ArrayList<>();
    List<SimpleTypeContainer> multiGeometrySimpleExtensions = new ArrayList<>();
    List<Object> multiGeometryObjectExtensions = new ArrayList<>();
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                {
                    final String eName = reader.getLocalName();
                    final String eUri = reader.getNamespaceURI();
                    // KML
                    if (equalsNamespace(eUri)) {
                        if (isAbstractGeometry(eName)) {
                            geometries.add(readAbstractGeometry(eName));
                        }
                    } else // EXTENSIONS
                    {
                        KmlExtensionReader r;
                        if ((r = this.getComplexExtensionReader(TAG_MULTI_GEOMETRY, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_MULTI_GEOMETRY, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.GEOMETRY.equals(extensionLevel)) {
                                abstractGeometryObjectExtensions.add(ext);
                            } else if (Extensions.Names.MULTI_GEOMETRY.equals(extensionLevel)) {
                                multiGeometryObjectExtensions.add(ext);
                            } else if (extensionLevel == null) {
                                if (ext instanceof AbstractGeometry) {
                                    geometries.add((AbstractGeometry) ext);
                                }
                            }
                        } else if ((r = getSimpleExtensionReader(TAG_MULTI_GEOMETRY, eUri, eName)) != null) {
                            Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_MULTI_GEOMETRY, eUri, eName);
                            Object ext = result.getKey();
                            Extensions.Names extensionLevel = result.getValue();
                            if (Extensions.Names.OBJECT.equals(extensionLevel)) {
                                objectSimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.GEOMETRY.equals(extensionLevel)) {
                                abstractGeometrySimpleExtensions.add((SimpleTypeContainer) ext);
                            } else if (Extensions.Names.MULTI_GEOMETRY.equals(extensionLevel)) {
                                multiGeometrySimpleExtensions.add((SimpleTypeContainer) ext);
                            }
                        }
                    }
                    break;
                }
            case XMLStreamConstants.END_ELEMENT:
                {
                    if (TAG_MULTI_GEOMETRY.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                        break boucle;
                    }
                    break;
                }
        }
    }
    return KmlReader.KML_FACTORY.createMultiGeometry(objectSimpleExtensions, idAttributes, abstractGeometrySimpleExtensions, abstractGeometryObjectExtensions, geometries, multiGeometrySimpleExtensions, multiGeometryObjectExtensions);
}
Also used : AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) ArrayList(java.util.ArrayList) LineString(org.geotoolkit.data.kml.model.LineString) SimpleTypeContainer(org.geotoolkit.data.kml.xsd.SimpleTypeContainer) Extensions(org.geotoolkit.data.kml.model.Extensions) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) Entry(java.util.Map.Entry)

Example 5 with AbstractGeometry

use of org.geotoolkit.data.kml.model.AbstractGeometry in project geotoolkit by Geomatys.

the class KmlReader method readPlacemark.

private Feature readPlacemark() throws XMLStreamException, KmlException, URISyntaxException {
    // AbstractObject
    List<SimpleTypeContainer> objectSimpleExtensions = new ArrayList<SimpleTypeContainer>();
    IdAttributes idAttributes = readIdAttributes();
    // AbstractFeature
    String name = null;
    boolean visibility = DEF_VISIBILITY;
    boolean open = DEF_OPEN;
    AtomPersonConstruct author = null;
    AtomLink link = null;
    String address = null;
    AddressDetails addressDetails = null;
    String phoneNumber = null;
    Object snippet = null;
    Object description = null;
    AbstractView view = null;
    AbstractTimePrimitive timePrimitive = null;
    URI styleUrl = null;
    List<AbstractStyleSelector> styleSelector = new ArrayList<AbstractStyleSelector>();
    Region region = null;
    Object extendedData = null;
    List<SimpleTypeContainer> featureSimpleExtensions = new ArrayList<SimpleTypeContainer>();
    List<Object> featureObjectExtensions = new ArrayList<Object>();
    // Placemark
    AbstractGeometry abstractGeometry = null;
    List<SimpleTypeContainer> placemarkSimpleExtensions = new ArrayList<SimpleTypeContainer>();
    List<Object> placemarkObjectExtensions = new ArrayList<Object>();
    boucle: while (reader.hasNext()) {
        switch(reader.next()) {
            case XMLStreamConstants.START_ELEMENT:
                final String eName = reader.getLocalName();
                final String eUri = reader.getNamespaceURI();
                // KML
                if (equalsNamespace(eUri)) {
                    switch(eName) {
                        case TAG_NAME:
                            name = reader.getElementText();
                            break;
                        case TAG_VISIBILITY:
                            visibility = parseBoolean(reader.getElementText());
                            break;
                        case TAG_OPEN:
                            open = parseBoolean(reader.getElementText());
                            break;
                        case TAG_ADDRESS:
                            address = reader.getElementText();
                            break;
                        case TAG_PHONE_NUMBER:
                            phoneNumber = reader.getElementText();
                            break;
                        case TAG_SNIPPET:
                            snippet = readElementText();
                            break;
                        case TAG_SNIPPET_BIG:
                            snippet = readSnippet();
                            break;
                        case TAG_DESCRIPTION:
                            description = readElementText();
                            break;
                        case TAG_STYLE_URL:
                            styleUrl = new URI(reader.getElementText());
                            break;
                        case TAG_REGION:
                            region = readRegion();
                            break;
                        case TAG_EXTENDED_DATA:
                            extendedData = readExtendedData();
                            break;
                        case TAG_META_DATA:
                            extendedData = readMetaData();
                            break;
                        default:
                            {
                                if (isAbstractView(eName)) {
                                    view = readAbstractView(eName);
                                } else if (isAbstractStyleSelector(eName)) {
                                    styleSelector.add(readAbstractStyleSelector(eName));
                                } else if (isAbstractTimePrimitive(eName)) {
                                    timePrimitive = readAbstractTimePrimitive(eName);
                                } else if (isAbstractGeometry(eName)) {
                                    abstractGeometry = readAbstractGeometry(eName);
                                }
                                break;
                            }
                    }
                } else // ATOM
                if (URI_ATOM.equals(eUri)) {
                    checkVersion(URI_KML_2_2);
                    // ABSTRACT FEATURE
                    if (TAG_ATOM_AUTHOR.equals(eName)) {
                        author = readAtomPersonConstruct();
                    } else if (TAG_ATOM_LINK.equals(eName)) {
                        link = readAtomLink();
                    }
                } else // XAL
                if (URI_XAL.equals(eUri)) {
                    checkVersion(URI_KML_2_2);
                    // ABSTRACT FEATURE
                    if (TAG_XAL_ADDRESS_DETAILS.equals(eName)) {
                        addressDetails = readXalAddressDetails();
                    }
                } else // EXTENSIONS
                {
                    KmlExtensionReader r;
                    if ((r = this.getComplexExtensionReader(TAG_PLACEMARK, eUri, eName)) != null) {
                        Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_PLACEMARK, eUri, eName);
                        Object ext = result.getKey();
                        Extensions.Names extensionLevel = result.getValue();
                        if (Extensions.Names.FEATURE.equals(extensionLevel)) {
                            featureObjectExtensions.add(ext);
                        } else if (Extensions.Names.PLACEMARK.equals(extensionLevel)) {
                            placemarkObjectExtensions.add(ext);
                        } else if (extensionLevel == null) {
                            if (ext instanceof AbstractGeometry) {
                                abstractGeometry = (AbstractGeometry) ext;
                            }
                        }
                    } else if ((r = getSimpleExtensionReader(TAG_PLACEMARK, eUri, eName)) != null) {
                        Entry<Object, Extensions.Names> result = r.readExtensionElement(URI_KML, TAG_PLACEMARK, eUri, eName);
                        Object ext = result.getKey();
                        Extensions.Names extensionLevel = result.getValue();
                        if (Extensions.Names.OBJECT.equals(extensionLevel)) {
                            objectSimpleExtensions.add((SimpleTypeContainer) ext);
                        } else if (Extensions.Names.FEATURE.equals(extensionLevel)) {
                            featureSimpleExtensions.add((SimpleTypeContainer) ext);
                        } else if (Extensions.Names.PLACEMARK.equals(extensionLevel)) {
                            placemarkSimpleExtensions.add((SimpleTypeContainer) ext);
                        }
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                if (TAG_PLACEMARK.equals(reader.getLocalName()) && containsNamespace(reader.getNamespaceURI())) {
                    break boucle;
                }
                break;
        }
    }
    return KmlReader.KML_FACTORY.createPlacemark(objectSimpleExtensions, idAttributes, name, visibility, open, author, link, address, addressDetails, phoneNumber, snippet, description, view, timePrimitive, styleUrl, styleSelector, region, extendedData, featureSimpleExtensions, featureObjectExtensions, abstractGeometry, placemarkSimpleExtensions, placemarkObjectExtensions);
}
Also used : AbstractView(org.geotoolkit.data.kml.model.AbstractView) AbstractGeometry(org.geotoolkit.data.kml.model.AbstractGeometry) AbstractTimePrimitive(org.geotoolkit.data.kml.model.AbstractTimePrimitive) AddressDetails(org.geotoolkit.xal.model.AddressDetails) AtomLink(org.geotoolkit.atom.model.AtomLink) ArrayList(java.util.ArrayList) LineString(org.geotoolkit.data.kml.model.LineString) SimpleTypeContainer(org.geotoolkit.data.kml.xsd.SimpleTypeContainer) Extensions(org.geotoolkit.data.kml.model.Extensions) URI(java.net.URI) AbstractStyleSelector(org.geotoolkit.data.kml.model.AbstractStyleSelector) IdAttributes(org.geotoolkit.data.kml.model.IdAttributes) AtomPersonConstruct(org.geotoolkit.atom.model.AtomPersonConstruct) Entry(java.util.Map.Entry) Region(org.geotoolkit.data.kml.model.Region)

Aggregations

AbstractGeometry (org.geotoolkit.data.kml.model.AbstractGeometry)9 ArrayList (java.util.ArrayList)6 LineString (org.geotoolkit.data.kml.model.LineString)4 LineString (org.locationtech.jts.geom.LineString)4 List (java.util.List)3 MultiGeometry (org.geotoolkit.data.kml.model.MultiGeometry)3 MultiLineString (org.locationtech.jts.geom.MultiLineString)3 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 Entry (java.util.Map.Entry)2 Boundary (org.geotoolkit.data.kml.model.Boundary)2 DefaultMultiGeometry (org.geotoolkit.data.kml.model.DefaultMultiGeometry)2 Extensions (org.geotoolkit.data.kml.model.Extensions)2 IdAttributes (org.geotoolkit.data.kml.model.IdAttributes)2 SimpleTypeContainer (org.geotoolkit.data.kml.xsd.SimpleTypeContainer)2 GeometryCollection (org.locationtech.jts.geom.GeometryCollection)2 Polygon (org.locationtech.jts.geom.Polygon)2 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1