Adapted coding style. Added the current version of the GPL 2.
This commit is contained in:
parent
62d1cd2843
commit
1f29891b5d
41
COPYING
41
COPYING
|
@ -1,12 +1,12 @@
|
||||||
GNU GENERAL PUBLIC LICENSE
|
GNU GENERAL PUBLIC LICENSE
|
||||||
Version 2, June 1991
|
Version 2, June 1991
|
||||||
|
|
||||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||||
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
Everyone is permitted to copy and distribute verbatim copies
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
of this license document, but changing it is not allowed.
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
Preamble
|
Preamble
|
||||||
|
|
||||||
The licenses for most software are designed to take away your
|
The licenses for most software are designed to take away your
|
||||||
freedom to share and change it. By contrast, the GNU General Public
|
freedom to share and change it. By contrast, the GNU General Public
|
||||||
|
@ -15,7 +15,7 @@ software--to make sure the software is free for all its users. This
|
||||||
General Public License applies to most of the Free Software
|
General Public License applies to most of the Free Software
|
||||||
Foundation's software and to any other program whose authors commit to
|
Foundation's software and to any other program whose authors commit to
|
||||||
using it. (Some other Free Software Foundation software is covered by
|
using it. (Some other Free Software Foundation software is covered by
|
||||||
the GNU Library General Public License instead.) You can apply it to
|
the GNU Lesser General Public License instead.) You can apply it to
|
||||||
your programs, too.
|
your programs, too.
|
||||||
|
|
||||||
When we speak of free software, we are referring to freedom, not
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
@ -55,8 +55,8 @@ patent must be licensed for everyone's free use or not licensed at all.
|
||||||
|
|
||||||
The precise terms and conditions for copying, distribution and
|
The precise terms and conditions for copying, distribution and
|
||||||
modification follow.
|
modification follow.
|
||||||
|
|
||||||
GNU GENERAL PUBLIC LICENSE
|
GNU GENERAL PUBLIC LICENSE
|
||||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
0. This License applies to any program or other work which contains
|
0. This License applies to any program or other work which contains
|
||||||
|
@ -110,7 +110,7 @@ above, provided that you also meet all of these conditions:
|
||||||
License. (Exception: if the Program itself is interactive but
|
License. (Exception: if the Program itself is interactive but
|
||||||
does not normally print such an announcement, your work based on
|
does not normally print such an announcement, your work based on
|
||||||
the Program is not required to print an announcement.)
|
the Program is not required to print an announcement.)
|
||||||
|
|
||||||
These requirements apply to the modified work as a whole. If
|
These requirements apply to the modified work as a whole. If
|
||||||
identifiable sections of that work are not derived from the Program,
|
identifiable sections of that work are not derived from the Program,
|
||||||
and can be reasonably considered independent and separate works in
|
and can be reasonably considered independent and separate works in
|
||||||
|
@ -168,7 +168,7 @@ access to copy from a designated place, then offering equivalent
|
||||||
access to copy the source code from the same place counts as
|
access to copy the source code from the same place counts as
|
||||||
distribution of the source code, even though third parties are not
|
distribution of the source code, even though third parties are not
|
||||||
compelled to copy the source along with the object code.
|
compelled to copy the source along with the object code.
|
||||||
|
|
||||||
4. You may not copy, modify, sublicense, or distribute the Program
|
4. You may not copy, modify, sublicense, or distribute the Program
|
||||||
except as expressly provided under this License. Any attempt
|
except as expressly provided under this License. Any attempt
|
||||||
otherwise to copy, modify, sublicense or distribute the Program is
|
otherwise to copy, modify, sublicense or distribute the Program is
|
||||||
|
@ -225,7 +225,7 @@ impose that choice.
|
||||||
|
|
||||||
This section is intended to make thoroughly clear what is believed to
|
This section is intended to make thoroughly clear what is believed to
|
||||||
be a consequence of the rest of this License.
|
be a consequence of the rest of this License.
|
||||||
|
|
||||||
8. If the distribution and/or use of the Program is restricted in
|
8. If the distribution and/or use of the Program is restricted in
|
||||||
certain countries either by patents or by copyrighted interfaces, the
|
certain countries either by patents or by copyrighted interfaces, the
|
||||||
original copyright holder who places the Program under this License
|
original copyright holder who places the Program under this License
|
||||||
|
@ -255,7 +255,7 @@ make exceptions for this. Our decision will be guided by the two goals
|
||||||
of preserving the free status of all derivatives of our free software and
|
of preserving the free status of all derivatives of our free software and
|
||||||
of promoting the sharing and reuse of software generally.
|
of promoting the sharing and reuse of software generally.
|
||||||
|
|
||||||
NO WARRANTY
|
NO WARRANTY
|
||||||
|
|
||||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||||
|
@ -277,9 +277,9 @@ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||||
POSSIBILITY OF SUCH DAMAGES.
|
POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
How to Apply These Terms to Your New Programs
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
If you develop a new program, and you want it to be of the greatest
|
If you develop a new program, and you want it to be of the greatest
|
||||||
possible use to the public, the best way to achieve this is to make it
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
@ -303,17 +303,16 @@ the "copyright" line and a pointer to where the full notice is found.
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
GNU General Public License for more details.
|
GNU General Public License for more details.
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License along
|
||||||
along with this program; if not, write to the Free Software
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
|
|
||||||
Also add information on how to contact you by electronic and paper mail.
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
If the program is interactive, make it output a short notice like this
|
If the program is interactive, make it output a short notice like this
|
||||||
when it starts in an interactive mode:
|
when it starts in an interactive mode:
|
||||||
|
|
||||||
Gnomovision version 69, Copyright (C) year name of author
|
Gnomovision version 69, Copyright (C) year name of author
|
||||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
This is free software, and you are welcome to redistribute it
|
This is free software, and you are welcome to redistribute it
|
||||||
under certain conditions; type `show c' for details.
|
under certain conditions; type `show c' for details.
|
||||||
|
@ -336,5 +335,5 @@ necessary. Here is a sample; alter the names:
|
||||||
This General Public License does not permit incorporating your program into
|
This General Public License does not permit incorporating your program into
|
||||||
proprietary programs. If your program is a subroutine library, you may
|
proprietary programs. If your program is a subroutine library, you may
|
||||||
consider it more useful to permit linking proprietary applications with the
|
consider it more useful to permit linking proprietary applications with the
|
||||||
library. If this is what you want to do, use the GNU Library General
|
library. If this is what you want to do, use the GNU Lesser General
|
||||||
Public License instead of this License.
|
Public License instead of this License.
|
||||||
|
|
26
ChangeLog
26
ChangeLog
|
@ -1,19 +1,23 @@
|
||||||
2013-11-07 Tobias Leupold <tobias.leupold@web.de> (Version 0.1.2)
|
2013-11-07 Tobias Leupold <tobias.leupold@web.de> (Version 0.1.2)
|
||||||
|
|
||||||
* Fixed crash when processing GPX files with empty or no path segments. Thanks to Fabian Seitz for the bug report!
|
* Fixed crash when processing GPX files with empty or no path segments.
|
||||||
|
Thanks to Fabian Seitz for the bug report!
|
||||||
* Added exception handler for non-readable or non-existant files or files with no valid GPX data.
|
|
||||||
|
* Added exception handler for non-readable or non-existant files or files with no valid GPX
|
||||||
|
data.
|
||||||
|
|
||||||
2012-08-26 Tobias Leupold <tobias.leupold@web.de> (Version 0.1.1)
|
2012-08-26 Tobias Leupold <tobias.leupold@web.de> (Version 0.1.1)
|
||||||
|
|
||||||
* Made the internal data structure simpler. Don't store the IDs of the tracks.
|
* Made the internal data structure simpler. Don't store the IDs of the tracks.
|
||||||
|
|
||||||
* Combine path segments before doing the Mercator projection, so that rounding errors can't affect the search.
|
* Combine path segments before doing the Mercator projection, so that rounding errors can't
|
||||||
|
affect the search.
|
||||||
* Made the algorithm to search for combinable paths more effective.
|
|
||||||
|
* Made the algorithm to search for combinable paths more effective.
|
||||||
* Draw circles instead of single points (that will not be shown). Added an command line option to drop such single points.
|
|
||||||
|
* Draw circles instead of single points (that will not be shown). Added an command line option
|
||||||
|
to drop such single points.
|
||||||
|
|
||||||
2012-08-25 Tobias Leupold <tobias.leupold@web.de> (Version 0.1)
|
2012-08-25 Tobias Leupold <tobias.leupold@web.de> (Version 0.1)
|
||||||
|
|
||||||
* Initial release
|
* Initial release
|
||||||
|
|
646
gpx2svg
646
gpx2svg
|
@ -25,358 +25,318 @@ import math
|
||||||
from xml.dom.minidom import parse as parseXml
|
from xml.dom.minidom import parse as parseXml
|
||||||
|
|
||||||
def parseGpx(gpxFile):
|
def parseGpx(gpxFile):
|
||||||
|
"""Get the latitude and longitude data of all track segments in a GPX file"""
|
||||||
"""Get the latitude and longitude data of all track segments in a GPX file"""
|
|
||||||
|
# Get the XML information
|
||||||
# Get the XML information
|
try:
|
||||||
try:
|
gpx = parseXml(gpxFile)
|
||||||
gpx = parseXml(gpxFile)
|
except IOError as error:
|
||||||
except IOError as error:
|
print('Error while reading file: %s. Terminating.' % error, file = sys.stderr)
|
||||||
print('Error while reading file: %s. Terminating.' % error, file = sys.stderr)
|
sys.exit(1)
|
||||||
sys.exit(1)
|
except:
|
||||||
except:
|
print('Error while parsing XML data:', file = sys.stderr)
|
||||||
print('Error while parsing XML data:', file = sys.stderr)
|
print(sys.exc_info(), file = sys.stderr)
|
||||||
print(sys.exc_info(), file = sys.stderr)
|
print('Terminating.', file = sys.stderr)
|
||||||
print('Terminating.', file = sys.stderr)
|
sys.exit(1)
|
||||||
sys.exit(1)
|
|
||||||
|
# Iterate over all tracks, track segments and points
|
||||||
# Iterate over all tracks, track segments and points
|
gpsData = []
|
||||||
|
for track in gpx.getElementsByTagName('trk'):
|
||||||
gpsData = []
|
for trackseg in track.getElementsByTagName('trkseg'):
|
||||||
|
trackSegData = []
|
||||||
for track in gpx.getElementsByTagName('trk'):
|
for point in trackseg.getElementsByTagName('trkpt'):
|
||||||
|
trackSegData.append(
|
||||||
for trackseg in track.getElementsByTagName('trkseg'):
|
(float(point.attributes['lon'].value), float(point.attributes['lat'].value))
|
||||||
|
)
|
||||||
trackSegData = []
|
# Leave out empty segments
|
||||||
|
if(trackSegData != []):
|
||||||
for point in trackseg.getElementsByTagName('trkpt'):
|
gpsData.append(trackSegData)
|
||||||
trackSegData.append((float(point.attributes['lon'].value), float(point.attributes['lat'].value)))
|
|
||||||
|
return gpsData
|
||||||
# Leave out empty segments
|
|
||||||
if(trackSegData != []):
|
|
||||||
gpsData.append(trackSegData)
|
|
||||||
|
|
||||||
return gpsData
|
|
||||||
|
|
||||||
def calcProjection(gpsData):
|
def calcProjection(gpsData):
|
||||||
|
"""Calculate a plane projection for a GPS dataset"""
|
||||||
"""Calculate a plane projection for a GPS dataset"""
|
|
||||||
|
projectedData = []
|
||||||
projectedData = []
|
for segment in gpsData:
|
||||||
|
projectedSegment = []
|
||||||
for segment in gpsData:
|
for coord in segment:
|
||||||
|
# At the moment, we only have the Mercator projection
|
||||||
projectedSegment = []
|
projectedSegment.append(mercatorProjection(coord))
|
||||||
|
projectedData.append(projectedSegment)
|
||||||
for coord in segment:
|
|
||||||
# At the moment, we only have the Mercator projection
|
return(projectedData)
|
||||||
projectedSegment.append(mercatorProjection(coord))
|
|
||||||
|
|
||||||
projectedData.append(projectedSegment)
|
|
||||||
|
|
||||||
return(projectedData)
|
|
||||||
|
|
||||||
def mercatorProjection(coord):
|
def mercatorProjection(coord):
|
||||||
|
"""Calculate the Mercator projection of a coordinate pair"""
|
||||||
"""Calculate the Mercator projection of a coordinate pair"""
|
|
||||||
|
# Assuming we're on earth, we have (according to GRS 80):
|
||||||
# Assuming we're on earth, we have (according to GRS 80):
|
r = 6378137.0
|
||||||
r = 6378137.0
|
|
||||||
|
# As long as meridian = 0 and can't be changed, we don't need:
|
||||||
# As long as meridian = 0 and can't be changed, we don't need:
|
# meridian = meridian * math.pi / 180.0
|
||||||
# meridian = meridian * math.pi / 180.0
|
# x = r * ((coord[0] * math.pi / 180.0) - meridian)
|
||||||
# x = r * ((coord[0] * math.pi / 180.0) - meridian)
|
|
||||||
# Instead, we use this simplified version:
|
# Instead, we use this simplified version:
|
||||||
x = r * coord[0] * math.pi / 180.0
|
x = r * coord[0] * math.pi / 180.0
|
||||||
|
y = r * math.log(math.tan((math.pi / 4.0) + ((coord[1] * math.pi / 180.0) / 2.0)))
|
||||||
y = r * math.log(math.tan((math.pi / 4.0) + ((coord[1] * math.pi / 180.0) / 2.0)))
|
return((x, y))
|
||||||
|
|
||||||
return((x, y))
|
|
||||||
|
|
||||||
def moveProjectedData(gpsData):
|
def moveProjectedData(gpsData):
|
||||||
|
"""Move a dataset to 0,0 and return it with the resulting width and height"""
|
||||||
"""Move a dataset to 0,0 and return it with the resulting width and height"""
|
|
||||||
|
# Find the minimum and maximum x and y coordinates
|
||||||
# Find the minimum and maximum x and y coordinates
|
minX = maxX = gpsData[0][0][0]
|
||||||
|
minY = maxY = gpsData[0][0][1]
|
||||||
minX = maxX = gpsData[0][0][0]
|
for segment in gpsData:
|
||||||
minY = maxY = gpsData[0][0][1]
|
for coord in segment:
|
||||||
|
if coord[0] < minX:
|
||||||
for segment in gpsData:
|
minX = coord[0]
|
||||||
for coord in segment:
|
if coord[0] > maxX:
|
||||||
if coord[0] < minX:
|
maxX = coord[0]
|
||||||
minX = coord[0]
|
if coord[1] < minY:
|
||||||
if coord[0] > maxX:
|
minY = coord[1]
|
||||||
maxX = coord[0]
|
if coord[1] > maxY:
|
||||||
if coord[1] < minY:
|
maxY = coord[1]
|
||||||
minY = coord[1]
|
|
||||||
if coord[1] > maxY:
|
# Move the GPS data to 0,0
|
||||||
maxY = coord[1]
|
movedGpsData = []
|
||||||
|
for segment in gpsData:
|
||||||
# Move the GPS data to 0,0
|
movedSegment = []
|
||||||
|
for coord in segment:
|
||||||
movedGpsData = []
|
movedSegment.append((coord[0] - minX, coord[1] - minY))
|
||||||
|
movedGpsData.append(movedSegment)
|
||||||
for segment in gpsData:
|
|
||||||
|
# Return the moved data and it's width and height
|
||||||
movedSegment = []
|
return(movedGpsData, maxX - minX, maxY - minY)
|
||||||
|
|
||||||
for coord in segment:
|
|
||||||
movedSegment.append((coord[0] - minX, coord[1] - minY))
|
|
||||||
|
|
||||||
movedGpsData.append(movedSegment)
|
|
||||||
|
|
||||||
# Return the moved data and it's width and height
|
|
||||||
return(movedGpsData, maxX - minX, maxY - minY)
|
|
||||||
|
|
||||||
def searchCircularSegments(gpsData):
|
def searchCircularSegments(gpsData):
|
||||||
|
"""Splits a GPS dataset to tracks that are circular and other tracks"""
|
||||||
"""Splits a GPS dataset to tracks that are circular and other tracks"""
|
|
||||||
|
circularSegments = []
|
||||||
circularSegments = []
|
straightSegments = []
|
||||||
straightSegments = []
|
|
||||||
|
for segment in gpsData:
|
||||||
for segment in gpsData:
|
if segment[0] == segment[len(segment) - 1]:
|
||||||
if segment[0] == segment[len(segment) - 1]:
|
circularSegments.append(segment)
|
||||||
circularSegments.append(segment)
|
else:
|
||||||
else:
|
straightSegments.append(segment)
|
||||||
straightSegments.append(segment)
|
|
||||||
|
return(circularSegments, straightSegments)
|
||||||
return(circularSegments, straightSegments)
|
|
||||||
|
|
||||||
def combineSegmentPairs(gpsData):
|
def combineSegmentPairs(gpsData):
|
||||||
|
"""Combine segment pairs to one bigger segment"""
|
||||||
"""Combine segment pairs to one bigger segment"""
|
|
||||||
|
combinedData = []
|
||||||
combinedData = []
|
|
||||||
|
# Walk through the GPS data and search for segment pairs
|
||||||
# Walk through the GPS data and search for segment pairs that end with the starting point of another track
|
# that end with the starting point of another track
|
||||||
|
while len(gpsData) > 0:
|
||||||
while len(gpsData) > 0:
|
# Get one segment from the source GPS data
|
||||||
|
firstTrackData = gpsData.pop()
|
||||||
# Get one segment from the source GPS data
|
foundMatch = False
|
||||||
firstTrackData = gpsData.pop()
|
|
||||||
|
# Try to find a matching segment
|
||||||
foundMatch = False
|
for i in range(len(gpsData)):
|
||||||
|
if firstTrackData[len(firstTrackData) - 1] == gpsData[i][0]:
|
||||||
# Try to find a matching segment
|
# There is a matching segment, so break here
|
||||||
for i in range(len(gpsData)):
|
foundMatch = True
|
||||||
if firstTrackData[len(firstTrackData) - 1] == gpsData[i][0]:
|
break
|
||||||
# There is a matching segment, so break here
|
|
||||||
foundMatch = True
|
if foundMatch == True:
|
||||||
break
|
# We found a pair of segments with one shared point, so pop the data of the second
|
||||||
|
# segment from the source GPS data and create a new segment containing all data, but
|
||||||
if foundMatch == True:
|
# without the overlapping point
|
||||||
# We found a pair of segments with one shared point, so pop the data of the second
|
firstTrackData.pop()
|
||||||
# segment from the source GPS data and create a new segment containing all data, but
|
combinedData.append(firstTrackData + gpsData[i])
|
||||||
# without the overlapping point
|
gpsData.pop(i)
|
||||||
firstTrackData.pop()
|
else:
|
||||||
combinedData.append(firstTrackData + gpsData[i])
|
# No segment with a shared point was found, so just append the data to the output
|
||||||
gpsData.pop(i)
|
combinedData.append(firstTrackData)
|
||||||
else:
|
|
||||||
# No segment with a shared point was found, so just append the data to the output
|
return(searchCircularSegments(combinedData))
|
||||||
combinedData.append(firstTrackData)
|
|
||||||
|
|
||||||
return(searchCircularSegments(combinedData))
|
|
||||||
|
|
||||||
def combineSegments(gpsData):
|
def combineSegments(gpsData):
|
||||||
|
"""Combine all segments of a GPS dataset that can be combined"""
|
||||||
"""Combine all segments of a GPS dataset that can be combined"""
|
|
||||||
|
# Search for circular segments. We can't combine them with any other segment.
|
||||||
# Search for circular segments. We can't combine them with any other segment.
|
circularSegments, remainingSegments = searchCircularSegments(gpsData)
|
||||||
circularSegments, remainingSegments = searchCircularSegments(gpsData)
|
|
||||||
|
# Search for segments that can be combined
|
||||||
# Search for segments that can be combined
|
while True:
|
||||||
|
# Look how many tracks we have now
|
||||||
while True:
|
segmentsBefore = len(remainingSegments)
|
||||||
|
|
||||||
# Look how many tracks we have now
|
# Search for segments that can be combined
|
||||||
segmentsBefore = len(remainingSegments)
|
newCircularSegments, remainingSegments = combineSegmentPairs(remainingSegments)
|
||||||
|
|
||||||
# Search for segments that can be combined
|
# Add newly found circular segments to processedSegments -- they can't be used anymore
|
||||||
newCircularSegments, remainingSegments = combineSegmentPairs(remainingSegments)
|
circularSegments = circularSegments + newCircularSegments
|
||||||
|
|
||||||
# Add newly found circular segments to processedSegments -- they can't be used anymore
|
if segmentsBefore == len(remainingSegments):
|
||||||
circularSegments = circularSegments + newCircularSegments
|
# combineSegmentPairs() did not reduce the number of tracks anymore,
|
||||||
|
# so we can't combine more tracks and can stop here
|
||||||
if segmentsBefore == len(remainingSegments):
|
break
|
||||||
# combineSegmentPairs() did not reduce the number of tracks anymore,
|
|
||||||
# so we can't combine more tracks and can stop here
|
return(circularSegments + remainingSegments)
|
||||||
break
|
|
||||||
|
|
||||||
return(circularSegments + remainingSegments)
|
|
||||||
|
|
||||||
def scaleCoords(coord, height, scale):
|
def scaleCoords(coord, height, scale):
|
||||||
"""Return a scaled pair of coordinates"""
|
"""Return a scaled pair of coordinates"""
|
||||||
return(coord[0] * scale, (coord[1] * -1 + height) * scale)
|
return(coord[0] * scale, (coord[1] * -1 + height) * scale)
|
||||||
|
|
||||||
def createCoordString(segment, height, scale):
|
def createCoordString(segment, height, scale):
|
||||||
|
"""Create the coordinate part of an SVG path string from a GPS data segment"""
|
||||||
"""Create the coordinate part of an SVG path string from a GPS data segment"""
|
|
||||||
|
coordString = ''
|
||||||
coordString = ''
|
for coord in segment:
|
||||||
|
x, y = scaleCoords(coord, height, scale)
|
||||||
for coord in segment:
|
coordString = coordString + ' ' + str(x) + ',' + str(y)
|
||||||
x, y = scaleCoords(coord, height, scale)
|
|
||||||
coordString = coordString + ' ' + str(x) + ',' + str(y)
|
return coordString
|
||||||
|
|
||||||
return coordString
|
|
||||||
|
|
||||||
def createPathString(drawCommand):
|
def createPathString(drawCommand):
|
||||||
"""Return a complete path element for a draw command string"""
|
"""Return a complete path element for a draw command string"""
|
||||||
return '<path d="' + drawCommand + '" style="fill:none;stroke:black" />\n'
|
return '<path d="' + drawCommand + '" style="fill:none;stroke:black" />\n'
|
||||||
|
|
||||||
def writeSvgData(gpsData, width, height, maxPixels, dropSinglePoints, outfile):
|
def writeSvgData(gpsData, width, height, maxPixels, dropSinglePoints, outfile):
|
||||||
|
"""Output the SVG data -- quick 'n' dirty, without messing around with dom stuff ;-)"""
|
||||||
"""Output the SVG data -- quick 'n' dirty, without messing around with dom stuff ;-)"""
|
|
||||||
|
# Calculate the scale factor we need to fit the requested maximal output size
|
||||||
# Calculate the scale factor we need to fit the requested maximal output size
|
if width <= maxPixels and height <= maxPixels:
|
||||||
if width <= maxPixels and height <= maxPixels:
|
scale = 1
|
||||||
scale = 1
|
elif width > height:
|
||||||
elif width > height:
|
scale = maxPixels / width
|
||||||
scale = maxPixels / width
|
else:
|
||||||
else:
|
scale = maxPixels / height
|
||||||
scale = maxPixels / height
|
|
||||||
|
# Open the requested output file or map to /dev/stdout
|
||||||
# Open the requested output file or map to /dev/stdout
|
if outfile != '/dev/stdout':
|
||||||
if outfile != '/dev/stdout':
|
fp = open(outfile, 'w')
|
||||||
fp = open(outfile, 'w')
|
else:
|
||||||
else:
|
fp = sys.stdout
|
||||||
fp = sys.stdout
|
|
||||||
|
# Header data
|
||||||
# Header data
|
fp.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
|
||||||
fp.write('<?xml version="1.0" encoding="UTF-8" standalone="no"?>\n')
|
fp.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
|
||||||
fp.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
|
fp.write(
|
||||||
fp.write('<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="%spx" height="%spx">\n' % (width * scale, height * scale))
|
'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="%spx" height="%spx">\n' % (
|
||||||
|
width * scale, height * scale
|
||||||
# Process all track segments and generate ids and path drawing commands for them
|
)
|
||||||
|
)
|
||||||
# First, we split the data to circular and straight segments
|
|
||||||
circularSegments, straightSegments = searchCircularSegments(gpsData)
|
# Process all track segments and generate ids and path drawing commands for them
|
||||||
|
|
||||||
realCircularSegments = []
|
# First, we split the data to circular and straight segments
|
||||||
singlePoints = []
|
circularSegments, straightSegments = searchCircularSegments(gpsData)
|
||||||
|
realCircularSegments = []
|
||||||
for segment in circularSegments:
|
singlePoints = []
|
||||||
|
for segment in circularSegments:
|
||||||
# We can leave out the last point, because it's equal to the first one
|
# We can leave out the last point, because it's equal to the first one
|
||||||
segment.pop()
|
segment.pop()
|
||||||
|
if len(segment) == 1:
|
||||||
if len(segment) == 1:
|
# It's a single point
|
||||||
# It's a single point
|
if dropSinglePoints == False:
|
||||||
if dropSinglePoints == False:
|
# We want to keep single points, so add it to singlePoints
|
||||||
# We want to keep single points, so add it to singlePoints
|
singlePoints.append(segment)
|
||||||
singlePoints.append(segment)
|
else:
|
||||||
else:
|
realCircularSegments.append(segment)
|
||||||
realCircularSegments.append(segment)
|
|
||||||
|
circularSegments = realCircularSegments
|
||||||
circularSegments = realCircularSegments
|
|
||||||
|
# Draw single points if requested
|
||||||
# Draw single points if requested
|
if len(singlePoints) > 0:
|
||||||
|
fp.write('<g>\n')
|
||||||
if len(singlePoints) > 0:
|
for segment in singlePoints:
|
||||||
|
x, y = scaleCoords(segment[0], height, scale)
|
||||||
fp.write('<g>\n')
|
fp.write('<circle cx="' + str(x) + '" cy="' + str(y) + '" r="0.5" style="stroke:none;fill:black"/>\n')
|
||||||
|
fp.write('</g>\n')
|
||||||
for segment in singlePoints:
|
|
||||||
x, y = scaleCoords(segment[0], height, scale)
|
# Draw all circular segments
|
||||||
fp.write('<circle cx="' + str(x) + '" cy="' + str(y) + '" r="0.5" style="stroke:none;fill:black"/>\n')
|
if len(circularSegments) > 0:
|
||||||
|
fp.write('<g>\n')
|
||||||
fp.write('</g>\n')
|
for segment in circularSegments:
|
||||||
|
fp.write(createPathString('M' + createCoordString(segment, height, scale) + ' Z'))
|
||||||
# Draw all circular segments
|
fp.write('</g>\n')
|
||||||
|
|
||||||
if len(circularSegments) > 0:
|
# Draw all un-closed paths
|
||||||
|
if len(straightSegments) > 0:
|
||||||
fp.write('<g>\n')
|
fp.write('<g>\n')
|
||||||
|
for segment in straightSegments:
|
||||||
for segment in circularSegments:
|
d = 'M' + createCoordString(segment, height, scale)
|
||||||
fp.write(createPathString('M' + createCoordString(segment, height, scale) + ' Z'))
|
fp.write(createPathString('M' + createCoordString(segment, height, scale)))
|
||||||
|
fp.write('</g>\n')
|
||||||
fp.write('</g>\n')
|
|
||||||
|
# Close the XML
|
||||||
# Draw all un-closed paths
|
fp.write('</svg>\n')
|
||||||
|
|
||||||
if len(straightSegments) > 0:
|
# Close the file if necessary
|
||||||
|
if fp != sys.stdout:
|
||||||
fp.write('<g>\n')
|
fp.close()
|
||||||
|
|
||||||
for segment in straightSegments:
|
|
||||||
d = 'M' + createCoordString(segment, height, scale)
|
|
||||||
fp.write(createPathString('M' + createCoordString(segment, height, scale)))
|
|
||||||
|
|
||||||
fp.write('</g>\n')
|
|
||||||
|
|
||||||
# Close the XML
|
|
||||||
fp.write('</svg>\n')
|
|
||||||
|
|
||||||
# Close the file if necessary
|
|
||||||
if fp != sys.stdout:
|
|
||||||
fp.close()
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
# Setup the command line argument parser
|
# Setup the command line argument parser
|
||||||
|
|
||||||
cmdArgParser = argparse.ArgumentParser(
|
cmdArgParser = argparse.ArgumentParser(
|
||||||
description = 'Convert GPX formatted geodata to Scalable Vector Graphics (SVG)',
|
description = 'Convert GPX formatted geodata to Scalable Vector Graphics (SVG)',
|
||||||
epilog = 'gpx2svg %s - http://nasauber.de/opensource/gpx2svg/' % __version__
|
epilog = 'gpx2svg %s - http://nasauber.de/opensource/gpx2svg/' % __version__
|
||||||
)
|
)
|
||||||
|
|
||||||
cmdArgParser.add_argument(
|
cmdArgParser.add_argument(
|
||||||
'-i', metavar = 'FILE', nargs = '?', type = str, default = '/dev/stdin',
|
'-i', metavar = 'FILE', nargs = '?', type = str, default = '/dev/stdin',
|
||||||
help = 'GPX input file (default: read from STDIN)'
|
help = 'GPX input file (default: read from STDIN)'
|
||||||
)
|
)
|
||||||
cmdArgParser.add_argument(
|
cmdArgParser.add_argument(
|
||||||
'-o', metavar = 'FILE', nargs = '?', type = str, default = '/dev/stdout',
|
'-o', metavar = 'FILE', nargs = '?', type = str, default = '/dev/stdout',
|
||||||
help = 'SVG output file (default: write to STDOUT)'
|
help = 'SVG output file (default: write to STDOUT)'
|
||||||
)
|
)
|
||||||
cmdArgParser.add_argument(
|
cmdArgParser.add_argument(
|
||||||
'-m', metavar = 'PIXELS', nargs = '?', type = int, default = 3000,
|
'-m', metavar = 'PIXELS', nargs = '?', type = int, default = 3000,
|
||||||
help = 'Maximum width or height of the SVG output in pixels (default: 3000)'
|
help = 'Maximum width or height of the SVG output in pixels (default: 3000)'
|
||||||
)
|
)
|
||||||
cmdArgParser.add_argument(
|
cmdArgParser.add_argument(
|
||||||
'-d', action = 'store_true',
|
'-d', action = 'store_true',
|
||||||
help = 'Drop single points (default: draw a circle with 1px diameter)'
|
help = 'Drop single points (default: draw a circle with 1px diameter)'
|
||||||
)
|
)
|
||||||
cmdArgParser.add_argument(
|
cmdArgParser.add_argument(
|
||||||
'-r', action = 'store_true',
|
'-r', action = 'store_true',
|
||||||
help = '"Raw" conversion: Create one SVG path per track segment, don\'t try to combine paths that end with the starting point of another path'
|
help = (
|
||||||
)
|
'"Raw" conversion: Create one SVG path per track segment, '
|
||||||
|
'don\'t try to combine paths that end with the starting point of another path'
|
||||||
# Get the given arguments
|
)
|
||||||
cmdArgs = cmdArgParser.parse_args()
|
)
|
||||||
|
|
||||||
# Map "-" to STDIN or STDOUT
|
# Get the given arguments
|
||||||
if cmdArgs.i == '-':
|
cmdArgs = cmdArgParser.parse_args()
|
||||||
cmdArgs.i = '/dev/stdin'
|
|
||||||
if cmdArgs.o == '-':
|
# Map "-" to STDIN or STDOUT
|
||||||
cmdArgs.o = '/dev/stdout'
|
if cmdArgs.i == '-':
|
||||||
|
cmdArgs.i = '/dev/stdin'
|
||||||
# Get the latitude and longitude data from the given GPX file or STDIN
|
if cmdArgs.o == '-':
|
||||||
gpsData = parseGpx(cmdArgs.i)
|
cmdArgs.o = '/dev/stdout'
|
||||||
|
|
||||||
# Check if we actually _have_ data
|
# Get the latitude and longitude data from the given GPX file or STDIN
|
||||||
if gpsData == []:
|
gpsData = parseGpx(cmdArgs.i)
|
||||||
print('No data to convert. Terminating.', file = sys.stderr)
|
|
||||||
sys.exit(1)
|
# Check if we actually _have_ data
|
||||||
|
if gpsData == []:
|
||||||
# Try to combine all track segments that can be combined if not requested otherwise
|
print('No data to convert. Terminating.', file = sys.stderr)
|
||||||
if not cmdArgs.r:
|
sys.exit(1)
|
||||||
gpsData = combineSegments(gpsData)
|
|
||||||
|
# Try to combine all track segments that can be combined if not requested otherwise
|
||||||
# Calculate a plane projection for a GPS dataset
|
if not cmdArgs.r:
|
||||||
# At the moment, we only have the Mercator projection
|
gpsData = combineSegments(gpsData)
|
||||||
gpsData = calcProjection(gpsData)
|
|
||||||
|
# Calculate a plane projection for a GPS dataset
|
||||||
# Move the projected data to the 0,0 origin of a cartesial coordinate system
|
# At the moment, we only have the Mercator projection
|
||||||
# and get the raw width and height of the resulting vector data
|
gpsData = calcProjection(gpsData)
|
||||||
gpsData, width, height = moveProjectedData(gpsData)
|
|
||||||
|
# Move the projected data to the 0,0 origin of a cartesial coordinate system
|
||||||
# Write the resulting SVG data to the requested output file or STDOUT
|
# and get the raw width and height of the resulting vector data
|
||||||
writeSvgData(gpsData, width, height, cmdArgs.m, cmdArgs.d, cmdArgs.o)
|
gpsData, width, height = moveProjectedData(gpsData)
|
||||||
|
|
||||||
|
# Write the resulting SVG data to the requested output file or STDOUT
|
||||||
|
writeSvgData(gpsData, width, height, cmdArgs.m, cmdArgs.d, cmdArgs.o)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue