Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KML write support #2244

Merged
merged 12 commits into from
Jun 27, 2014
Merged

KML write support #2244

merged 12 commits into from
Jun 27, 2014

Conversation

elemoine
Copy link
Member

This PR adds "write" support to the KML format. You can see it in action at http://erilem.net/ol3/kmlwrite/examples/kml.html.

Current limitations:

  • The KML format's writeFeatures function does not transform the features' geometries. This means that the application developer needs to do the transform (to WGS84) by himself, as done in the kml example. This is consistent with the GeoJSON and GPX formats, which do not transform the data either. This problem should therefore be addressed separately, with an API that is consistent across feature formats.
  • The KML format's writeFeatures function does not serialize feature properties that are not KML properties. For example a feature property named "description" will be serialized, while a property named "arbitraryname" won't. ExtendedData can be used in the future to address that limitation.

The code was written by @oterral and myself.

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

I can't open up the output with Oxygen

screen shot 2014-06-25 at 12 43 03

@elemoine
Copy link
Member Author

@bartvde thanks for testing @bartvde. We have encoding issues there.

@elemoine
Copy link
Member Author

@bartvde, do you mind trying again?

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

I can open up it now @elemoine

what XML schema is this supposed to validate against? It does not validate against KML 2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd

@elemoine
Copy link
Member Author

http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd

Yes, that's the schema.

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

then you should use:

xmlns="http://www.opengis.net/kml/2.2"

instead, but many validation errors, I assume this might be because of the extensions

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

SystemID: /Users/bartvandeneijnden/Downloads/download (1).kml
Engine name: Xerces
Severity: error
Description: cvc-complex-type.2.4.a: Invalid content was found starting with element 'Style'. One of '{"http://www.opengis.net/kml/2.2":PlacemarkSimpleExtensionGroup, "http://www.opengis.net/kml/2.2":PlacemarkObjectExtensionGroup}' is expected.
Start location: 8:14
End location: 8:19
URL: http://www.w3.org/TR/xmlschema-1/#cvc-complex-type
SystemID: /Users/bartvandeneijnden/Downloads/download (1).kml
Engine name: Xerces
Severity: error
Description: cvc-complex-type.2.4.a: Invalid content was found starting with element 'gx:w'. One of '{"http://www.opengis.net/kml/2.2":BasicLinkSimpleExtensionGroup, "http://www.opengis.net/kml/2.2":BasicLinkObjectExtensionGroup}' is expected.
Start location: 12:26
End location: 12:30
URL: http://www.w3.org/TR/xmlschema-1/#cvc-complex-type

all errors look like the above 2

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

Style goes before the geometry, so sequence should be:

        <Placemark>
            <name>Adelbodenstrasse</name>
            <styleUrl>#style-Secondary</styleUrl>            
            <Style>
                <IconStyle>
                    <Icon>
                        <href>https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                    </Icon>
                    <hotSpot x="2" y="12" xunits="pixels" yunits="pixels"/>
                </IconStyle>
                <LineStyle>
                    <color>ff17e8ea</color>
                    <width>4</width>
                </LineStyle>
                <PolyStyle>
                    <color>ffffffff</color>
                </PolyStyle>
            </Style>
            <LineString>
                <coordinates>7.602502200000001,46.54975689999998,1
                    7.6023929,46.54962560000001,1</coordinates>
            </LineString>            
        </Placemark>

this gets rid of the first error, still need to look into the gx: one

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

okay it validates fine against: https://developers.google.com/kml/schema/kml22gx.xsd which is what we need I guess since of the gx: tags, so only sequence of tags needs changing as mentioned above. I think we should output a reference to the schema as well to avoid confusion.

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document>
        <Placemark>
            <name>Adelbodenstrasse</name>
            <styleUrl>#style-Secondary</styleUrl>            
            <Style>
                <IconStyle>
                    <Icon>
                        <href>https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                        <gx:w>32</gx:w>
                        <gx:h>32</gx:h>                        
                    </Icon>
                    <hotSpot x="2" y="12" xunits="pixels" yunits="pixels"/>
                </IconStyle>
                <LineStyle>
                    <color>ff17e8ea</color>
                    <width>4</width>
                </LineStyle>
                <PolyStyle>
                    <color>ffffffff</color>
                </PolyStyle>
            </Style>
            <LineString>
                <coordinates>7.602502200000001,46.54975689999998,1
                    7.6023929,46.54962560000001,1</coordinates>
            </LineString>            
        </Placemark>
    </Document>
</kml>

@elemoine
Copy link
Member Author

Thanks Bart. So do we need to change the xmlns?

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

@elemoine yes it needs changing in order to validate, the following validates for me and which is what I would probably output:

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.opengis.net/kml/2.2
    https://developers.google.com/kml/schema/kml22gx.xsd">
    <Document>
        <Placemark>
            <name>Adelbodenstrasse</name>
            <styleUrl>#style-Secondary</styleUrl>            
            <Style>
                <IconStyle>
                    <Icon>
                        <href>https://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
                        <gx:w>32</gx:w>
                        <gx:h>32</gx:h>                        
                    </Icon>
                    <hotSpot x="2" y="12" xunits="pixels" yunits="pixels"/>
                </IconStyle>
                <LineStyle>
                    <color>ff17e8ea</color>
                    <width>4</width>
                </LineStyle>
                <PolyStyle>
                    <color>ffffffff</color>
                </PolyStyle>
            </Style>
            <LineString>
                <coordinates>7.602502200000001,46.54975689999998,1
                    7.6023929,46.54962560000001,1</coordinates>
            </LineString>            
        </Placemark>
    </Document>
</kml>

@probins
Copy link
Contributor

probins commented Jun 25, 2014

writeFeatures function does not serialize feature properties that are KML properties

um, think you mean "...that are not KML properties"?

@elemoine
Copy link
Member Author

um, think you mean "...that are not KML properties"?

Yes, I updated the comment. Thanks.

@elemoine
Copy link
Member Author

@bartvde are you sure about the first url in the schemaLocation? It's not a reference to an xsd file.

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

yes I'm sure that's the URI that the schema applies to see eg http://www.w3schools.com/schema/schema_schema.asp

@elemoine
Copy link
Member Author

I see. That makes more sense now.

@elemoine
Copy link
Member Author

Branch updated.

  • 7fb3e58 adds an exampleNS.strToBase64 function that can convert utf8 strings to base64. The code is copied from mdn. This function is used in the kml example.
  • 384f2a0 changes the attributes we set in the <kml> node to produce valid documents. Thanks @bartvde for your help on this.

http://erilem.net/ol3/kmlwrite/examples/kml.html is up-to-date.

@probins
Copy link
Contributor

probins commented Jun 25, 2014

I'm wondering whether it makes sense to have an example, given that this will probably change because of the transform issue. I don't think any of the other formats have an example for write, do they?

@elemoine
Copy link
Member Author

I'm wondering whether it makes sense to have an example, given that this will probably change because of the transform issue.

It will just slightly change.

I don't think any of the other formats have an example for write, do they?

No, but they probably should. I think it's good to have examples for features provided by the library. And in this case I see some value in having an example that shows how to make locally generated files downloadable (the <a download="map.kml"> stuff).

@elemoine
Copy link
Member Author

this will not work in IE, see: 87ea5fa#diff-497c3a47c9cdc970f191c9e4e852833c

Thanks again Bart! Does this every node.setAttributeNS occurrences should be changed to ol.xml.setAttributeNS?

@bartvde
Copy link
Member

bartvde commented Jun 25, 2014

no I think only the ones with the 'xmlns' prefix

@probins
Copy link
Contributor

probins commented Jun 25, 2014

they probably should

I like the OL2 vector-formats example, where you could read/write in any of the supported formats. Useful for testing as well as being a demo

@oterral
Copy link
Contributor

oterral commented Jun 26, 2014

I've changed the node.setAttributeNS occurrences for xmlns attribute and reordering correctly placemark's child nodes.

The validation is now successfull with oxygen.

@bartvde
Copy link
Member

bartvde commented Jun 26, 2014

great, I love valid XML ;-)

@oterral
Copy link
Contributor

oterral commented Jun 26, 2014

👍

@elemoine
Copy link
Member Author

Thanks a lot @oterral. This looks good. I've updated http://erilem.net/ol3/kmlwrite/examples/kml.html. @bartvde please tell us if this is good to merge to you.

@bartvde
Copy link
Member

bartvde commented Jun 26, 2014

I'll have a quick look now

*/
ol.format.KML.XSD_URIS_ = [
'http://www.opengis.net/kml/2.2',
'https://developers.google.com/kml/schema/kml22gx.xsd'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the xsd is in here, since the xsd is not a (namespace) URI.

In other formats we have used schemaLocation: https://github.com/openlayers/ol3/blob/master/src/ol/format/wfsformat.js#L83:L89 instead

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I did things this way initially because I did not understand that part very well. I'll add a commit that mimics what's in the wfs format.

@bartvde
Copy link
Member

bartvde commented Jun 26, 2014

Nice work guys, no more comments from me.

@elemoine
Copy link
Member Author

Thanks for the thorough review. I'll add commits based on your review comments.

@elemoine elemoine added this to the v3.0.0-gamma.2 milestone Jun 26, 2014
@elemoine
Copy link
Member Author

I've just added new commits that take @bartvde's comments into account. I'll merge when Travis is all green.

@bartvde
Copy link
Member

bartvde commented Jun 27, 2014

thanks for addressing this @elemoine

elemoine pushed a commit that referenced this pull request Jun 27, 2014
@elemoine elemoine merged commit be69f5b into openlayers:master Jun 27, 2014
@elemoine elemoine deleted the kmlwrite branch June 27, 2014 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants