Python – כיצד להמיר KML ל-CSV? – פינקסטר

Share on facebook
Share on twitter
Share on linkedin
Share on telegram
Share on whatsapp
Share on email
פרסומת
תכנון תשתיות רפואיות


מה זה KML?

ℹ️ הַגדָרָה: ה שפת סימון חור מפתח (KML) הוא פורמט קובץ להצגת נתונים גיאוגרפיים ב-Google Earth או מה שנקרא "דפדפני כדור הארץ". בדומה ל-XML, KML משתמש במבנה מבוסס תגים עם אלמנטים ותכונות מקוננות.

כיצד להמיר KML ל-CSV ב-Python?

אתה יכול להמיר א .kml אל א .csv קובץ ב-Python באמצעות ה מרק יפה וה csv ספריות. אתה משתמש בראשון כדי לקרוא את קובץ ה-KML בנוי ב-XML ובשני כדי לכתוב את קובץ ה-CSV שורה אחר שורה.

להלן דוגמה לקוד בהשראת אך שונה מ זֶה מאגר GitHub. אתה יכול להעתיק ולהדביק אותו בספרייה שבה נמצא קובץ ה-KML שלך ולשנות את שמות קבצי הקלט והפלט בהתחלה כדי להמיר את ה-KML שלך ל-CSV ב-Python:

from bs4 import BeautifulSoup
import csv


infile="my_file.kml"
outfile="my_file.csv"


with open(infile, 'r') as f:
    s = BeautifulSoup(f, 'xml')
    
    with open(outfile, 'wb') as csvfile:
        writer = csv.writer(csvfile)

        for coords in s.find_all('coordinates'):
            
            # Take coordinate string from KML and break it up into [Lat,Lon,Lat,Lon...] to get CSV row
            space_splits = coords.string.split(" ")
            row = []
            
            for split in space_splits[1:]:
                # Note: because of the space between <coordinates>" "-80.123, we slice [1:]
                comma_split = split.split(',')

                # lattitude
                row.append(comma_split[1])
                
                # longitude
                row.append(comma_split[0])
            
            writer.writerow(row)

המרה לדוגמה

אנו משתמשים בדברים הבאים לִטעוֹם קובץ KML as 'my_file.kml':

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>KML Samples</name>
    <open>1</open>
    <description>Unleash your creativity with the help of these examples!</description>
    <Style id="downArrowIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.google.com/mapfiles/kml/pal4/icon28.png</href>
        </Icon>
      </IconStyle>
    </Style>
    <Style id="globeIcon">
      <IconStyle>
        <Icon>
          <href>http://maps.google.com/mapfiles/kml/pal3/icon19.png</href>
        </Icon>
      </IconStyle>
      <LineStyle>
        <width>2</width>
      </LineStyle>
    </Style>
    <Style id="transPurpleLineGreenPoly">
      <LineStyle>
        <color>7fff00ff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Style id="yellowLineGreenPoly">
      <LineStyle>
        <color>7f00ffff</color>
        <width>4</width>
      </LineStyle>
      <PolyStyle>
        <color>7f00ff00</color>
      </PolyStyle>
    </Style>
    <Style id="thickBlackLine">
      <LineStyle>
        <color>87000000</color>
        <width>10</width>
      </LineStyle>
    </Style>
    <Style id="redLineBluePoly">
      <LineStyle>
        <color>ff0000ff</color>
      </LineStyle>
      <PolyStyle>
        <color>ffff0000</color>
      </PolyStyle>
    </Style>
    <Style id="blueLineRedPoly">
      <LineStyle>
        <color>ffff0000</color>
      </LineStyle>
      <PolyStyle>
        <color>ff0000ff</color>
      </PolyStyle>
    </Style>
    <Style id="transRedPoly">
      <LineStyle>
        <width>1.5</width>
      </LineStyle>
      <PolyStyle>
        <color>7d0000ff</color>
      </PolyStyle>
    </Style>
    <Style id="transBluePoly">
      <LineStyle>
        <width>1.5</width>
      </LineStyle>
      <PolyStyle>
        <color>7dff0000</color>
      </PolyStyle>
    </Style>
    <Style id="transGreenPoly">
      <LineStyle>
        <width>1.5</width>
      </LineStyle>
      <PolyStyle>
        <color>7d00ff00</color>
      </PolyStyle>
    </Style>
    <Style id="transYellowPoly">
      <LineStyle>
        <width>1.5</width>
      </LineStyle>
      <PolyStyle>
        <color>7d00ffff</color>
      </PolyStyle>
    </Style>
    <Style id="noDrivingDirections">
      <BalloonStyle>
        <text><![CDATA[
          <b>$[name]</b>
          <br /><br />
          $5/5 - (1 vote) What is KML? ℹ️ Definition: The Keyhole Markup Language (KML) is a file format for displaying geographic data in Google Earth or other so-called “Earth Browsers”. Similarly to XML, KML uses a tag-based structure with nested elements and attributes. How to Convert KML to CSV in Python? You can convert a ... Read more
        ]]></text>
      </BalloonStyle>
    </Style>
    <Folder>
      <name>Placemarks</name>
      <description>These are just some of the different kinds of placemarks with
        which you can mark your favorite places</description>
      <LookAt>
        <longitude>-122.0839597145766</longitude>
        <latitude>37.42222904525232</latitude>
        <altitude>0</altitude>
        <heading>-148.4122922628044</heading>
        <tilt>40.5575073395506</tilt>
        <range>500.6566641072245</range>
      </LookAt>
      <Placemark>
        <name>Simple placemark</name>
        <description>Attached to the ground. Intelligently places itself at the
          height of the underlying terrain.</description>
        <Point>
          <coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
        </Point>
      </Placemark>
      <Placemark>
        <name>Floating placemark</name>
        <visibility>0</visibility>
        <description>Floats a defined distance above the ground.</description>
        <LookAt>
          <longitude>-122.0839597145766</longitude>
          <latitude>37.42222904525232</latitude>
          <altitude>0</altitude>
          <heading>-148.4122922628044</heading>
          <tilt>40.5575073395506</tilt>
          <range>500.6566641072245</range>
        </LookAt>
        <styleUrl>#downArrowIcon</styleUrl>
        <Point>
          <altitudeMode>relativeToGround</altitudeMode>
          <coordinates>-122.084075,37.4220033612141,50</coordinates>
        </Point>
      </Placemark>
      <Placemark>
        <name>Extruded placemark</name>
        <visibility>0</visibility>
        <description>Tethered to the ground by a customizable
          &quot;tail&quot;</description>
        <LookAt>
          <longitude>-122.0845787421525</longitude>
          <latitude>37.42215078737763</latitude>
          <altitude>0</altitude>
          <heading>-148.4126684946234</heading>
          <tilt>40.55750733918048</tilt>
          <range>365.2646606980322</range>
        </LookAt>
        <styleUrl>#globeIcon</styleUrl>
        <Point>
          <extrude>1</extrude>
          <altitudeMode>relativeToGround</altitudeMode>
          <coordinates>-122.0857667006183,37.42156927867553,50</coordinates>
        </Point>
      </Placemark>
    </Folder>
    <Folder>
      <name>Styles and Markup</name>
      <visibility>0</visibility>
      <description>With KML it is easy to create rich, descriptive markup to
        annotate and enrich your placemarks</description>
      <LookAt>
        <longitude>-122.0845787422371</longitude>
        <latitude>37.42215078726837</latitude>
        <altitude>0</altitude>
        <heading>-148.4126777488172</heading>
        <tilt>40.55750733930874</tilt>
        <range>365.2646826292919</range>
      </LookAt>
      <styleUrl>#noDrivingDirections</styleUrl>
      <Document>
        <name>Highlighted Icon</name>
        <visibility>0</visibility>
        <description>Place your mouse over the icon to see it display the new
          icon</description>
        <LookAt>
          <longitude>-122.0856552124024</longitude>
          <latitude>37.4224281311035</latitude>
          <altitude>0</altitude>
          <heading>0</heading>
          <tilt>0</tilt>
          <range>265.8520424250024</range>
        </LookAt>
        <Style id="highlightPlacemark">
          <IconStyle>
            <Icon>
              <href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href>
            </Icon>
          </IconStyle>
        </Style>
        <Style id="normalPlacemark">
          <IconStyle>
            <Icon>
              <href>http://maps.google.com/mapfiles/kml/paddle/wht-blank.png</href>
            </Icon>
          </IconStyle>
        </Style>
        <StyleMap id="exampleStyleMap">
          <Pair>
            <key>normal</key>
            <styleUrl>#normalPlacemark</styleUrl>
          </Pair>
          <Pair>
            <key>highlight</key>
            <styleUrl>#highlightPlacemark</styleUrl>
          </Pair>
        </StyleMap>
        <Placemark>
          <name>Roll over this icon</name>
          <visibility>0</visibility>
          <styleUrl>#exampleStyleMap</styleUrl>
          <Point>
            <coordinates>-122.0856545755255,37.42243077405461,0</coordinates>
          </Point>
        </Placemark>
      </Document>
      <Placemark>
        <name>Descriptive HTML</name>
        <visibility>0</visibility>
        <description><![CDATA[Click on the blue link!<br><br>
Placemark descriptions can be enriched by using many standard HTML tags.<br>
For example:
<hr>
Styles:<br>
<i>Italics</i>, 
<b>Bold</b>, 
<u>Underlined</u>, 
<s>Strike Out</s>, 
subscript<sub>subscript</sub>, 
superscript<sup>superscript</sup>, 
<big>Big</big>, 
<small>Small</small>, 
<tt>Typewriter</tt>, 
<em>Emphasized</em>, 
<strong>Strong</strong>, 
<code>Code</code>
<hr>
Fonts:<br> 
<font color="red">red by name</font>, 
<font color="#408010">leaf green by hexadecimal RGB</font>
<br>
<font size=1>size 1</font>, 
<font size=2>size 2</font>, 
<font size=3>size 3</font>, 
<font size=4>size 4</font>, 
<font size=5>size 5</font>, 
<font size=6>size 6</font>, 
<font size=7>size 7</font>
<br>
<font face=times>Times</font>, 
<font face=verdana>Verdana</font>, 
<font face=arial>Arial</font><br>
<hr>
Links: 
<br>
<a href="http://earth.google.com/">Google Earth!</a>
<br>
 or:  Check out our website at www.google.com
<hr>
Alignment:<br>
<p align=left>left</p>
<p align=center>center</p>
<p align=right>right</p>
<hr>
Ordered Lists:<br>
<ol><li>First</li><li>Second</li><li>Third</li></ol>
<ol type="a"><li>First</li><li>Second</li><li>Third</li></ol>
<ol type="A"><li>First</li><li>Second</li><li>Third</li></ol>
<hr>
Unordered...



קישור לכתבת המקור – 2022-08-18 18:19:59

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on telegram
Telegram
Share on whatsapp
WhatsApp
Share on email
Email
פרסומת
MAGNEZIX מגנזיקס

עוד מתחומי האתר