Exemple #1
0
    def test_getXmlWithCDATA(self):
        '''tests the format_as_cdata function'''
        from pykml.util import format_xml_with_cdata

        kmlobj = KML.kml(
            KML.Document(
                KML.Placemark(KML.name('foobar'), KML.styleUrl('#big_label'),
                              KML.description('<html>'), KML.text('<html>'),
                              KML.linkDescription('<html>'),
                              KML.displayName('<html>'))))
        self.assertEqual(
            etree.tostring(format_xml_with_cdata(kmlobj)),
            '<kml xmlns:gx="http://www.google.com/kml/ext/2.2"'
            ' xmlns:atom="http://www.w3.org/2005/Atom"'
            ' xmlns="http://www.opengis.net/kml/2.2">'
            '<Document>'
            '<Placemark>'
            '<name>foobar</name>'
            '<styleUrl>#big_label</styleUrl>'
            '<description><![CDATA[<html>]]></description>'
            '<text><![CDATA[<html>]]></text>'
            '<linkDescription><![CDATA[<html>]]></linkDescription>'
            '<displayName><![CDATA[<html>]]></displayName>'
            '</Placemark>'
            '</Document>'
            '</kml>')
Exemple #2
0
 def test_getXmlWithCDATA(self):
     '''tests the format_as_cdata function'''
     from pykml.util import format_xml_with_cdata
     
     kmlobj = KML.kml(
         KML.Document(
             KML.Placemark(
                 KML.name('foobar'),
                 KML.styleUrl('#big_label'),
                 KML.description('<html>'),
                 KML.text('<html>'),
                 KML.linkDescription('<html>'),
                 KML.displayName('<html>')
             )
         )
     )
     self.assertEqual(
         etree.tostring(format_xml_with_cdata(kmlobj)),
         '<kml xmlns:gx="http://www.google.com/kml/ext/2.2"'
                       ' xmlns:atom="http://www.w3.org/2005/Atom"'
                       ' xmlns="http://www.opengis.net/kml/2.2">'
           '<Document>'
             '<Placemark>'
               '<name>foobar</name>'
               '<styleUrl>#big_label</styleUrl>'
               '<description><![CDATA[<html>]]></description>'
               '<text><![CDATA[<html>]]></text>'
               '<linkDescription><![CDATA[<html>]]></linkDescription>'
               '<displayName><![CDATA[<html>]]></displayName>'
             '</Placemark>'
           '</Document>'
         '</kml>'
     )
Exemple #3
0
    def test_getXmlWithCDATA(self):
        """tests the format_as_cdata function"""
        from pykml.util import format_xml_with_cdata

        kml_obj = KML.kml(
            KML.Document(
                KML.Placemark(
                    KML.name('foobar'),
                    KML.styleUrl('#big_label'),
                    KML.description('<html>'),
                    KML.text('<html>'),
                    KML.linkDescription('<html>'),
                    KML.displayName('<html>')
                )
            )
        )

        root = format_xml_with_cdata(kml_obj)

        data = etree.tostring(root, encoding='utf-8', xml_declaration=True)
        expected = \
            '<?xml version="1.0" encoding="UTF-8"?>' \
            '<kml xmlns:gx="http://www.google.com/kml/ext/2.2" ' \
            'xmlns:atom="http://www.w3.org/2005/Atom" ' \
            'xmlns="http://www.opengis.net/kml/2.2">' \
            '<Document>' \
            '<Placemark>' \
            '<name>foobar</name>' \
            '<styleUrl>#big_label</styleUrl>' \
            '<description><![CDATA[<html>]]></description>' \
            '<text><![CDATA[<html>]]></text>' \
            '<linkDescription><![CDATA[<html>]]></linkDescription>' \
            '<displayName><![CDATA[<html>]]></displayName>' \
            '</Placemark>' \
            '</Document>' \
            '</kml>'
        expected = expected.encode('utf-8')

        self.assertXmlEquivalentOutputs(data, expected)
Exemple #4
0
        alert_msg=alert_msg,
        reserve_phone=reserve_phone,
        info_phone=info_phone,
        email=email,
        rv_checkout_checkin=rv_checkout_checkin,
        address_1=address_1,
        address_2=address_2,
        rating=rating,
    )

    # note: this is a free and simple geocoder but not the most accurate
    # google might be best but costs $$
    geocode_result = geocoder.arcgis(", ".join([address_1, address_2]))
    latlng_str = ",".join(
        [str(_) for _ in geocode_result.current_result.latlng[::-1]])
    placemark = KML.Placemark(
        KML.name(name),
        KML.description(description),
        KML.Point(KML.coordinates(latlng_str), ),
    )
    folder.append(placemark)

with open("koa-campgrounds.kml", "w") as fh:
    fh.write(
        etree.tostring(
            format_xml_with_cdata(folder),
            method="xml",
            encoding="unicode",
            pretty_print=True,
        ))
Exemple #5
0
    def test_convert_csv_to_kml(self):
        """Tests the convert_csv_to_kml function"""
        import tempfile
        from pykml.util import convert_csv_to_kml
        from pykml.util import format_xml_with_cdata

        # create a CSV file for testing
        csvfile = tempfile.TemporaryFile()
        csvfile.write('name,snippet,lat,lon\n')
        csvfile.write('first,The first one,45.0,-90.0\n')
        csvfile.write('second,The second one,46.0,-89.0\n')
        csvfile.write('third,"The third one (with quotes)",45.0,-88.0\n')
        csvfile.seek(0)

        kmldoc = convert_csv_to_kml(csvfile)

        csvfile.close()

        self.assertEqual(
            etree.tostring(format_xml_with_cdata(kmldoc)),
            '<kml xmlns:gx="http://www.google.com/kml/ext/2.2" '
            'xmlns:atom="http://www.w3.org/2005/Atom" '
            'xmlns="http://www.opengis.net/kml/2.2">'
            '<Document>'
            '<Folder>'
            '<name>KmlFile</name>'
            '<Placemark>'
            '<name>first</name>'
            '<Snippet maxLines="2">The first one</Snippet>'
            '<description>'
            '<![CDATA['
            '<table border="1"'
            '<tr><th>snippet</th><td>The first one</td></tr>'
            '<tr><th>lat</th><td>45.0</td></tr>'
            '<tr><th>lon</th><td>-90.0</td></tr>'
            '<tr><th>name</th><td>first</td></tr>'
            '</table>'
            ']]>'
            '</description>'
            '<Point>'
            '<coordinates>-90.0,45.0</coordinates>'
            '</Point>'
            '</Placemark>'
            '<Placemark>'
            '<name>second</name>'
            '<Snippet maxLines="2">The second one</Snippet>'
            '<description><![CDATA[<table border="1"<tr><th>snippet</th><td>The second one</td></tr><tr><th>lat</th><td>46.0</td></tr><tr><th>lon</th><td>-89.0</td></tr><tr><th>name</th><td>second</td></tr></table>]]></description>'
            '<Point>'
            '<coordinates>-89.0,46.0</coordinates>'
            '</Point>'
            '</Placemark>'
            '<Placemark>'
            '<name>third</name>'
            '<Snippet maxLines="2">The third one (with quotes)</Snippet>'
            '<description><![CDATA[<table border="1"<tr><th>snippet</th><td>The third one (with quotes)</td></tr><tr><th>lat</th><td>45.0</td></tr><tr><th>lon</th><td>-88.0</td></tr><tr><th>name</th><td>third</td></tr></table>]]></description>'
            '<Point>'
            '<coordinates>-88.0,45.0</coordinates>'
            '</Point>'
            '</Placemark>'
            '</Folder>'
            '</Document>'
            '</kml>')
Exemple #6
0
 def test_convert_csv_to_kml(self):
     """Tests the convert_csv_to_kml function"""
     import tempfile
     from pykml.util import convert_csv_to_kml
     from pykml.util import format_xml_with_cdata
     
     # create a CSV file for testing
     csvfile = tempfile.TemporaryFile()
     csvfile.write('name,snippet,lat,lon\n')
     csvfile.write('first,The first one,45.0,-90.0\n')
     csvfile.write('second,The second one,46.0,-89.0\n')
     csvfile.write('third,"The third one (with quotes)",45.0,-88.0\n')
     csvfile.seek(0)
     
     kmldoc = convert_csv_to_kml(csvfile)
     
     csvfile.close()
     
     self.assertEqual(
         etree.tostring(format_xml_with_cdata(kmldoc)),
         '<kml xmlns:gx="http://www.google.com/kml/ext/2.2" '
              'xmlns:atom="http://www.w3.org/2005/Atom" '
              'xmlns="http://www.opengis.net/kml/2.2">'
              '<Document>'
                 '<Folder>'
                     '<name>KmlFile</name>'
                     '<Placemark>'
                         '<name>first</name>'
                         '<Snippet maxLines="2">The first one</Snippet>'
                         '<description>'
                             '<![CDATA['
                               '<table border="1"'
                                 '<tr><th>snippet</th><td>The first one</td></tr>'
                                 '<tr><th>lat</th><td>45.0</td></tr>'
                                 '<tr><th>lon</th><td>-90.0</td></tr>'
                                 '<tr><th>name</th><td>first</td></tr>'
                               '</table>'
                             ']]>'
                         '</description>'
                         '<Point>'
                             '<coordinates>-90.0,45.0</coordinates>'
                         '</Point>'
                     '</Placemark>'
                     '<Placemark>'
                         '<name>second</name>'
                         '<Snippet maxLines="2">The second one</Snippet>'
                         '<description><![CDATA[<table border="1"<tr><th>snippet</th><td>The second one</td></tr><tr><th>lat</th><td>46.0</td></tr><tr><th>lon</th><td>-89.0</td></tr><tr><th>name</th><td>second</td></tr></table>]]></description>'
                         '<Point>'
                             '<coordinates>-89.0,46.0</coordinates>'
                         '</Point>'
                     '</Placemark>'
                     '<Placemark>'
                         '<name>third</name>'
                         '<Snippet maxLines="2">The third one (with quotes)</Snippet>'
                         '<description><![CDATA[<table border="1"<tr><th>snippet</th><td>The third one (with quotes)</td></tr><tr><th>lat</th><td>45.0</td></tr><tr><th>lon</th><td>-88.0</td></tr><tr><th>name</th><td>third</td></tr></table>]]></description>'
                         '<Point>'
                             '<coordinates>-88.0,45.0</coordinates>'
                         '</Point>'
                     '</Placemark>'
                 '</Folder>'
             '</Document>'
         '</kml>'
     )
Exemple #7
0
    def test_convert_csv_to_kml(self):
        """Tests the convert_csv_to_kml function"""
        import tempfile
        from pykml.util import convert_csv_to_kml
        from pykml.util import format_xml_with_cdata

        # create a CSV file for testing
        with tempfile.NamedTemporaryFile(mode='wt', suffix='.csv', delete=False) as csvfile:
            csvfile_name = csvfile.name
            csvfile.write('name,snippet,lat,lon\n')
            csvfile.write('first,The first one,45.0,-90.0\n')
            csvfile.write('second,The second one,46.0,-89.0\n')
            csvfile.write('third,"The third one (with quotes)",45.0,-88.0\n')

        with open(csvfile_name, 'rt') as csvfile:
            kmldoc = convert_csv_to_kml(csvfile)

        os.unlink(csvfile_name)

        root = format_xml_with_cdata(kmldoc)

        data = etree.tostring(root, encoding='utf-8', xml_declaration=True)
        expected = \
            '<?xml version="1.0" encoding="UTF-8"?>' \
            '<kml xmlns="http://www.opengis.net/kml/2.2" ' \
            'xmlns:atom="http://www.w3.org/2005/Atom" ' \
            'xmlns:gx="http://www.google.com/kml/ext/2.2">' \
            '<Document>' \
            '<Folder>' \
            '<name>KmlFile</name>' \
            '<Placemark>' \
            '<name>first</name>' \
            '<Snippet maxLines="2">The first one</Snippet>' \
            '<description><![CDATA[<table border="1"' \
            '<tr><th>name</th><td>first</td></tr>' \
            '<tr><th>snippet</th><td>The first one</td></tr>' \
            '<tr><th>lat</th><td>45.0</td></tr>' \
            '<tr><th>lon</th><td>-90.0</td></tr>' \
            '</table>]]></description>' \
            '<Point>' \
            '<coordinates>-90.0,45.0</coordinates>' \
            '</Point>' \
            '</Placemark>' \
            '<Placemark>' \
            '<name>second</name>' \
            '<Snippet maxLines="2">The second one</Snippet>' \
            '<description><![CDATA[<table border="1"' \
            '<tr><th>name</th><td>second</td></tr>' \
            '<tr><th>snippet</th><td>The second one</td></tr>' \
            '<tr><th>lat</th><td>46.0</td></tr>' \
            '<tr><th>lon</th><td>-89.0</td></tr>' \
            '</table>]]></description>' \
            '<Point>' \
            '<coordinates>-89.0,46.0</coordinates>' \
            '</Point>' \
            '</Placemark>' \
            '<Placemark>' \
            '<name>third</name>' \
            '<Snippet maxLines="2">The third one (with quotes)</Snippet>' \
            '<description><![CDATA[<table border="1"' \
            '<tr><th>name</th><td>third</td></tr>' \
            '<tr><th>snippet</th><td>The third one (with quotes)</td></tr>' \
            '<tr><th>lat</th><td>45.0</td></tr>' \
            '<tr><th>lon</th><td>-88.0</td></tr>' \
            '</table>]]></description>' \
            '<Point>' \
            '<coordinates>-88.0,45.0</coordinates>' \
            '</Point>' \
            '</Placemark>' \
            '</Folder>' \
            '</Document>' \
            '</kml>'
        expected = expected.encode('utf-8')

        self.assertXmlEquivalentOutputs(data, expected)
def main():
    """
    Create a KML document with a folder and a style for each earthquake
    magnitude
    """
    doc = KML.Document()

    icon_styles = [
        [2, 'ff000000'],
        [3, 'ffff0000'],
        [4, 'ff00ff55'],
        [5, 'ffff00aa'],
        [6, 'ff00ffff'],
        [7, 'ff0000ff'],
    ]

    # create a series of Icon Styles
    for threshold, color in icon_styles:
        doc.append(
            KML.Style(
                KML.IconStyle(
                    KML.color(color),
                    KML.scale(threshold / 2),
                    KML.Icon(
                        KML.href('http://maps.google.com/mapfiles/kml/shapes/earthquake.png'),
                    ),
                    KML.hotSpot(x='0.5', y='0', xunits='fraction', yunits='fraction'),
                ),
                get_balloon_style(),
                id='earthquake-style-{threshold}'.format(threshold=threshold),
            )
        )

    doc.append(KML.Folder())

    # read in a csv file, and create a placemark for each record
    url = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.csv'
    fileobject = urlopen(url)

    if six.PY3:  # fileobject is bytes, csv requires string
        import codecs
        fileobject = codecs.getreader('utf-8')(fileobject)

    for row in csv.DictReader(fileobject):
        timestamp = datetime.strptime(row['time'], '%Y-%m-%dT%H:%M:%S.%fZ')
        pm = KML.Placemark(
            KML.name('Magnitude={0}'.format(row['mag'])),
            KML.TimeStamp(
                KML.when(timestamp.strftime('%Y-%m-%dT%H:%M:%SZ')),
            ),
            KML.styleUrl(
                '#earthquake-style-{thresh}'.format(
                    thresh=int(float(row['mag']))
                )
            ),
            make_extended_data_elements(row),
            KML.Point(
                KML.coordinates('{0},{1}'.format(row['longitude'], row['latitude']))
            )
        )
        doc.Folder.append(pm)

    # check if the schema is valid
    schema_gx = Schema('kml22gx.xsd')
    schema_gx.assertValid(doc)

    kml = KML.kml(doc)

    print(etree.tostring(format_xml_with_cdata(kml),
                         pretty_print=True,
                         encoding='utf-8',
                         xml_declaration=True).decode())