User:Mr. Matté/Create KMLs from line data

In order to create a KML file from .shp data following this tutorial, you will need a program to turn .shp data into .kml such as ArcGIS or QGIS, Google Earth, and a simple text editor like Notepad. Sorry if this is too wordy, feel free to edit if you have a better way to explain this.

Let's say you're part of the 1.0*10-46% of people in the world working on creating KML files for road articles. You may be following the Tutorial to create these files but run into the problem of the fact that the road data is split into multiple line segments. If you just save the KML file like that and upload it to an article, you'll get ugly maps like this [1]. The following is how I handle this problem so that you can end up with a single line segment like this: [2]

  1. Download the data. For major highways, I get the .shp data from the TIGER files from the U.S. Census. Go to this page: [3], select Roads from the dropdown menu, and select the desired state under Primary and Secondary Roads. Save and unzip that file to the desired location.
  2. Select the right segments. Open ArcGIS/QGIS and import the desired vector layer. Usually, you can go to the attribute table, sort the features by "FULLNAME," usually you can find the desired highway under "I-XX," "US Hwy XX," or "Ste Hwy XX" so if you select your desired features there, that should be enough.
  3. Export these segments to a KML file. When the right features are selected, save them to a KML file. In QGIS, right click on the line vector layer and select "Save Selection As..." Save that to the desired location. I recommend skipping the attribute creation. You won't need the GIS viewer anymore.
  4. Inspect the line segments. Open the KML file in Google Earth and go to the folder containing the line segments. There is likely multiple lines, all with a 1 pt-width and red stroke. Right click the segment and open up the elevation profile. On the chart that opens, go to the left side and watch where the red arrow is on the globe. As you move the cursor right along the chart, watch where the arrow moves. In the U.S. road articles, the standard order of directions are south to north or west to east. If the arrow on the globe moves S-->N or W-->E as you move right on the chart, this segment is "good." If the opposite, somehow mark this segment as being in reverse (I would rename the segment as "Reverse"). Do this for all the segments in the folder.
  5. Order the segments. Go to the southern/western end of the whole road and select the segment that appears at that end. In the folder, bring this segment to the top of the list. Go to the next segment and drag it to the second position, etc. It's helpful to hide the segment after it's correctly ordered in the folder. Right click on the whole folder and save this as either the same or a different KML file.
  6. Manually edit the file.
  • Right click the new/newly saved KML file, and open it in Notepad; pages of coordinates and occasionally <tags enclosed in brackets> should appear. If there are some segments that were reversed, first you will have to reorder the coordinates. It's not as hard as you'd think.
  • Go to the block of coordinates that had the reversed direction (if you renamed the line, look for <name>Reverse</name>, the coordinates will follow that), highlight all the points between <coordinates> and </coordinates>, cut/Ctrl+X them, and paste them onto the front page of [4].
  • Set the "Items Are Separated By" and "I Want Items Separated By" radio buttons to "Space," and then hit the "↑REVERSE LIST" button on the top right hand side of the page.
  • Select all, copy and paste the recently-sorted block of coordinates back into the KML text file in Notepad.
  • Repeat for all reversed segments
  • Once all segments are in the correct direction, go to each instance of a line of code like this: "</coordinates> </LineString> </Placemark> <Placemark> <styleUrl>#sn_ylw-pushpin</styleUrl> <LineString> <coordinates>" and delete that. Only do this for lines in the middle, don't delete the first set of "<Placemark><LineString><coordinates>" or the last set of "</Placemark></LineString></coordinates>".
  • Save this file and try to open it again in Google Earth. If done correctly, there should be only one line segment.
7. Inspect the line. Do a quick run through of the whole line to make sure that it actually overlaps the road. You may need to fix some points in Google Earth if the road was realigned, extended, or had any other funny stuff done to it. Save the single line segment (not the folder) as a KML file, open it again in Notepad, select all, and paste it into the appropriate Template:Attached KML/Whatever. Q.E.D.