本文整理汇总了Python中pykml.parser.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: kml2pykml
def kml2pykml():
"Parse a KML file and generates a pyKML script"
import urllib2
from pykml.parser import parse
from optparse import OptionParser
parser = OptionParser(
usage="usage: %prog FILENAME_or_URL",
version="%prog 0.1",
)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("wrong number of arguments")
else:
uri = args[0]
try:
with open(uri) as f:
doc = parse(f, schema=None)
except IOError:
try:
f = urllib2.urlopen(uri)
doc = parse(f, schema=None)
finally:
#pass
try:
f
except NameError:
pass #variable was not defined
else:
f.close
print write_python_script_for_kml_document(doc)
示例2: test_parse_kml_document
def test_parse_kml_document(self):
"""Tests the parsing of an valid KML file object"""
test_kml = b'<kml xmlns="http://www.opengis.net/kml/2.2"/>'
fileobject = BytesIO(test_kml)
schema = Schema('ogckml22.xsd')
tree = parse(fileobject, schema=schema)
self.assertEqual(etree.tostring(tree), test_kml)
tree = parse(fileobject, schema=schema)
self.assertEqual(etree.tostring(tree), test_kml)
示例3: test_parse_kml_file_with_cdata
def test_parse_kml_file_with_cdata(self):
"Tests the parsing of a local KML file, with a CDATA description string"
test_datafile = path.join(
path.dirname(__file__),
'testfiles',
'google_kml_tutorial/using_the_cdata_element.kml'
)
# parse with validation
with open(test_datafile) as f:
doc = parse(f, schema=Schema('ogckml22.xsd'))
self.assertEquals(
etree.tostring(doc),
'<kml xmlns="http://www.opengis.net/kml/2.2">'
'<Document>'
'<Placemark>'
'<name>CDATA example</name>'
'<description>'
'<![CDATA[\n'
' <h1>CDATA Tags are useful!</h1>\n'
' <p><font color="red">Text is <i>more readable</i> and \n'
' <b>easier to write</b> when you can avoid using entity \n'
' references.</font></p>\n'
' ]]>'
'</description>'
'<Point>'
'<coordinates>102.595626,14.996729</coordinates>'
'</Point>'
'</Placemark>'
'</Document>'
'</kml>'
)
# parse without validation
with open(test_datafile) as f:
doc2 = parse(f)
self.assertEquals(
etree.tostring(doc2),
'<kml xmlns="http://www.opengis.net/kml/2.2">'
'<Document>'
'<Placemark>'
'<name>CDATA example</name>'
'<description>'
'<![CDATA[\n'
' <h1>CDATA Tags are useful!</h1>\n'
' <p><font color="red">Text is <i>more readable</i> and \n'
' <b>easier to write</b> when you can avoid using entity \n'
' references.</font></p>\n'
' ]]>'
'</description>'
'<Point>'
'<coordinates>102.595626,14.996729</coordinates>'
'</Point>'
'</Placemark>'
'</Document>'
'</kml>'
)
示例4: test_parse_kml_file
def test_parse_kml_file(self):
"Tests the parsing of a local KML file, with validation"
test_datafile = path.join(
path.dirname(__file__),
'testfiles',
'google_kml_developers_guide/complete_tour_example.kml'
)
# parse with validation
with open(test_datafile) as f:
doc = parse(f, schema=Schema('kml22gx.xsd'))
# parse without validation
with open(test_datafile) as f:
doc = parse(f)
self.assertTrue(True)
示例5: test_parse_invalid_ogc_kml_document
def test_parse_invalid_ogc_kml_document(self):
"""Tests the parsing of an invalid KML document. Note that this KML
document uses elements that are not in the OGC KML spec.
"""
url = 'https://developers.google.com/kml/documentation/kmlfiles/altitudemode_reference.kml'
context = ssl._create_unverified_context()
try:
with urlopen(url, context=context) as fileobject:
with self.assertRaises(etree.XMLSyntaxError):
# tree =
parse(fileobject, schema=Schema('ogckml22.xsd'))
except URLError:
print('Unable to access the URL. Skipping test...')
示例6: process
def process():
shapes = ['shape_id', 'shape_pt_lat', 'shape_pt_lon', 'shape_pt_sequence']
# Open system.kml file in go/data/routes
with open('{}/route/kml/shapes.kml'.format(DATA_PATH)) as file:
doc = parser.parse(file)
shape = None
for t in doc.getiterator():
# If the tag is a description, find the shape_id in the CDATA
if re.sub('\{.*\}', '', t.tag).lower() == 'description':
shape = int(parse_cdata(t.text))
if shape in Segment.objects:
shape = Shape(shape) if shape not in Shape.objects else Shape.objects[shape]
else:
print('Shape {} does not have a matching segment.'.format(shape))
shape = None
# Save coordinates
if re.sub('\{.*\}', '', t.tag).lower() == 'coordinates' and shape:
# A new Path must be created for the discovered coordinates
shape.add_path([re.split(',', x) for x in re.split('\s', t.text) if len(re.split(',', x)) == 3])
# Open writer
writer = open('{}/gtfs/files/shapes.txt'.format(REPORT_PATH), 'w')
writer.write('{}\n'.format(','.join(shapes)))
for obj in sorted(Shape.objects.keys()):
shape = Shape.objects[obj]
shape.order_paths()
for path, reverse in shape.order:
for point in path.get_points(reverse):
writer.write('{}\n'.format(','.join([str(s) for s in [shape.id, point[1], point[0], shape.index]])))
shape.index += 1
示例7: _load_waypoints
def _load_waypoints(kml_stream):
"""Loads and returns the waypoints from a KML string."""
def get_child(element, tag_name):
"""Returns the child element with the given tag name."""
try:
return getattr(element, tag_name)
except AttributeError:
raise ValueError('No {tag} element found'.format(tag=tag_name))
root = parser.parse(kml_stream).getroot()
if 'kml' not in root.tag:
raise ValueError('Not a KML file')
document = get_child(root, 'Document')
placemark = get_child(document, 'Placemark')
line_string = get_child(placemark, 'LineString')
# Unlike all of the other tag names, "coordinates" is not capitalized
coordinates = get_child(line_string, 'coordinates')
waypoints = []
text = coordinates.text.strip()
for csv in re.split(r'\s', text):
(
longitude,
latitude,
altitude # pylint: disable=unused-variable
) = csv.split(',')
waypoints.append((
Telemetry.longitude_to_m_offset(float(longitude), float(latitude)),
Telemetry.latitude_to_m_offset(float(latitude))
))
return waypoints
示例8: get_boundaries
def get_boundaries():
""" Parse the KML file to get the boundaries
"""
with open(kml_file) as f:
tree = parser.parse(f)
root = tree.getroot()
N = 0
placemarks = {}
for ch in root.Document.Folder.getchildren():
if 'Placemark' in ch.tag:
# Found a placemark
N += 1
pname = int(ch.name.text)
# Get the coordinates
pcoords = ch.MultiGeometry.Polygon.outerBoundaryIs.LinearRing.coordinates.text
pcoords = pcoords.strip()
pcoords = re.split(',| ', pcoords)
pcoords = [float(c) for c in pcoords]
lons = pcoords[0::3]
lats = pcoords[1::3]
assert len(lons) == len(lats)
#print "Polygon has %d vertices" % len(lons)
placemarks[pname] = (lats, lons)
print "Found %d placemarks" % N
return placemarks
示例9: checkKML
def checkKML(filename):
with open(filename) as f:
doc = parser.parse(f)
if schema_ogc.validate(doc) or schema_gx.validate(doc):
return True
else:
return False
示例10: buildCwbStaticStationDict
def buildCwbStaticStationDict():
url = 'http://www.cwb.gov.tw/wwwgis/kml/cwbobs.kml'
#url = 'http://code.google.com/apis/kml/documentation/KML_Samples.kml'
fileobject = urlopen(url)
root = parser.parse(fileobject).getroot()
cwbStaticStationDict={}
for i in range(100):
try:
k=root.Document.Placemark[i]
stationName=k.name.text[0:len(k.name.text)-9]
stationNumber=k.name.text[-5:]
longitude=float(k.LookAt.longitude.text)
latitude=float(k.LookAt.latitude.text)
altitude=float(k.LookAt.altitude.text)
#print stationName,stationNumber,longitude,latitude,altitude
cwbStaticStationDict[stationName.encode('utf-8')]=[stationNumber,longitude,latitude,altitude]
except IndexError:
pass
#print i, 'index out of range'
#check if dic is OK
#print cwbStaticStationDict['金門'], len(cwbStaticStationDict)
return(cwbStaticStationDict)
示例11: readOrigFolder
def readOrigFolder(foldername):
if not os.path.exists(foldername):
return {}
onlyfiles = [f for f in os.listdir(foldername) if os.path.isfile(os.path.join(foldername, f))]
result = {}
for f in onlyfiles:
fullpath = os.path.join(foldername, f)
with open(fullpath) as kmlfile:
kmldoc = parser.parse(kmlfile)
root = kmldoc.getroot()
for x in root.Document.Folder.Placemark:
# print etree.tostring(x, pretty_print=True)
coords = [y for y in str(x.LineString.coordinates).rstrip().lstrip().split(" ")]
coords_filtered = []
for y in coords:
lat, lon, h = y.split(",")
coords_filtered.append([float(lat), float(lon)])
#if "Path" in str(x.name) or "Line" in x.name or "," in str(x.name) or "." in str(x.name):
# print x.name
try:
name = str(x.name).replace("Path", "").replace("Line", "").replace(",", "").replace(".", "").rstrip().lstrip()
#if int(name) in result.keys():
# print "Error: key in orig already exists!", name, result[int(name)], coords
result[int(name)] = coords_filtered
except:
print "##################", fullpath, x.name
return result
示例12: getImagesFromGSV
def getImagesFromGSV(kmlfile):
svdirectory = "streetviewimages"
if not os.path.exists(svdirectory):
os.makedirs(svdirectory)
family = os.path.basename(kmlfile).split('.')[0]
fov=120
pitches = [5]#,-11
with open(kmlfile) as f:
doc = parser.parse(f)
root = doc.getroot()
for placemark in root.Document.Placemark:
pt = placemark.find(".//{http://earth.google.com/kml/2.1}Point")
name = placemark.name
if pt is not None:
lon,lat,x = placemark.Point.coordinates.text.strip().split(',')
for data in placemark.ExtendedData.findall(".//*[@name='headings']/{http://earth.google.com/kml/2.1}value"):
headings = data.text.strip().split(",")
h_count = 0
for h in headings:
for pitch in pitches:
imgname = str(family) + '_' + str(name) + '_' + str(h_count) + '.jpg'
p = os.path.join(svdirectory,imgname)
if not os.path.isfile(p):
gsv_base_url = "http://maps.googleapis.com/maps/api/streetview?size=640x360&location="+str(lat)+","+str(lon)
gsv_url = gsv_base_url + "&fov="+str(fov) + "&heading="+str(h) + "&pitch=" + str(pitch) +"&key=" + GSV_KEY
urllib.urlretrieve(gsv_url, p)
print "Downloaded", p
else:
print p, "Image already exist"
h_count+=1
示例13: extract_tracks
def extract_tracks(f):
from pykml import parser
doc = parser.parse(f)
root = doc.getroot()
tracks = find_tracks(root.Document.Placemark)
return tracks
示例14: extractKML
def extractKML( ridingID, path ) :
kmlFileName = path + 'ridingMapData_' + str( ridingID ) + '.kml'
with open(kmlFileName, 'r') as f:
doc = parser.parse(f)
print "Opening riding map file:", ridingID
poll_list = []
pol = {}
firstPoll = True
for element in doc.iter("{http://www.opengis.net/kml/2.2}ExtendedData", "{http://www.opengis.net/kml/2.2}Polygon" ):
if element.tag == "{http://www.opengis.net/kml/2.2}ExtendedData":
if firstPoll == False:
poll_list.append( pol.copy())
firstPoll = False
pol = {}
parseData( pol, element )
elif element.tag == "{http://www.opengis.net/kml/2.2}Polygon": # or element.tag == "{http://www.opengis.net/kml/2.2}MultiGeometry":
#pprint( pol )
parsePolygon( pol, element )
poll_list.append( pol.copy())
f.close()
riding = {}
riding['ridingID'] = ridingID
riding['num_polls'] = len(poll_list)
riding['polls'] = poll_list
outputFile = path + 'ridingMapData_' + str( ridingID ) + '.json'
with open(outputFile, 'w') as f:
json.dump(riding, f)
f.close()
print "Successfully wrote riding map file:", ridingID
outputFile = path + 'ridingMapBoundaryData_' + str( ridingID ) + '.json'
boundary = {}
boundary['ridingID'] = ridingID
boundary['coords'] = calculateRidingBoundary( poll_list )
with open(outputFile, 'w') as f:
json.dump(boundary, f)
f.close()
print "Successfully wrote riding map boundary file:", ridingID
return True
示例15: get_station_objects
def get_station_objects(start_year=1980, end_year=2010, sel_names=None):
# read ice depth values
df = get_obs_data()
lon_min, lon_max = -100, 0
lat_min, lat_max = 40, 90
nvals_min = 100
p = parser.parse(STATION_COORDS_FILE.open())
root = p.getroot()
station_elts = root.Document.Placemark
# select points based on the lat/lon limits?
stations = []
for el in station_elts:
lon, lat, _ = [float(c.strip()) for c in el.Point.coordinates.text.split(",")]
# Check if the station
if sel_names is not None:
is_ok = False
for sel_name in sel_names:
if sel_name.lower() in el.name.text.lower():
is_ok = True
break
if not is_ok:
continue
if (lon_min <= lon <= lon_max) and (lat_min <= lat <= lat_max):
print("{}: {}".format(el.name, el.Point.coordinates))
df_s = df.loc[df.station_name.str.lower().str.startswith(el.name.text.lower())]
df_s = df_s.loc[(df_s.year >= start_year) & (df_s.year <= end_year)]
if len(df_s) < nvals_min:
continue
print(len(df_s))
d_to_v = dict(zip(df_s["Date"][:], df_s["ice_depth"][:]))
# df_s.plot(x="Date", y="ice_depth")
# plt.title(el.name.text)
# plt.show()
# print(df_s.station_name)
stations.append(Station(st_id=df_s.station_name.iloc[0], lon=lon, lat=lat, date_to_value=d_to_v))
return stations