• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python parser.parse函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pykml.parser.parse函数的典型用法代码示例。如果您正苦于以下问题:Python parse函数的具体用法?Python parse怎么用?Python parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了parse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:123abcde,项目名称:pykml,代码行数:31,代码来源:factory.py


示例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)
开发者ID:recombinant,项目名称:pykml,代码行数:9,代码来源:test_parser.py


示例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>'
     )
开发者ID:123abcde,项目名称:pykml,代码行数:55,代码来源:test_parser.py


示例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)
开发者ID:giricgoyal,项目名称:CS526_UIC_Fall2013,代码行数:14,代码来源:test_parser.py


示例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...')
开发者ID:recombinant,项目名称:pykml,代码行数:14,代码来源:test_parser.py


示例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
开发者ID:mlockwood,项目名称:go_transit_src,代码行数:34,代码来源:shape_kml.py


示例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
开发者ID:bskari,项目名称:sparkfun-avc,代码行数:34,代码来源:simple_waypoint_generator.py


示例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
开发者ID:PerryZh,项目名称:pyhawkes,代码行数:30,代码来源:parse_community_areas.py


示例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
开发者ID:Gimpneek,项目名称:maptoapp,代码行数:7,代码来源:maptoapp.py


示例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)
开发者ID:Gordon-Yiu,项目名称:Scrap-Cwb-of-Taiwan,代码行数:26,代码来源:ScrapCwbToGetTwWeatherList.py


示例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
开发者ID:darty,项目名称:neighborhood-dashboard,代码行数:27,代码来源:road_distance_calculator.py


示例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
开发者ID:darty,项目名称:neighborhood-dashboard,代码行数:30,代码来源:create_ui_streetsigns.py


示例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
开发者ID:akbargumbira,项目名称:angkot,代码行数:8,代码来源:import_transportation_kml.py


示例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
开发者ID:mullinsean,项目名称:riding_analytics,代码行数:58,代码来源:extractKML2007.py


示例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
开发者ID:guziy,项目名称:RPN,代码行数:56,代码来源:validate_ice_depth_with_Canadan_Lake_Ice_Database.py


示例16: kml2shapely

def kml2shapely(kmlfile):
    with open(kmlfile) as f:
        doc = parser.parse(f)
    root = doc.getroot()
    coords = root.Document.Placemark.Polygon.outerBoundaryIs.LinearRing.coordinates
    str1 = (str(coords)).split(' ')[0]
    allcoords = re.findall('-?\d+\.*\d*', str(coords))
    coordarray = np.array(allcoords, dtype=float).reshape(len(allcoords)/3, 3)[:,:2]
    ring = GeoPolygon(LONG_LAT, *coordarray.transpose())
    return ring
开发者ID:kezilu,项目名称:pextant,代码行数:10,代码来源:utils.py


示例17: test_parse_invalid_kml_document

 def test_parse_invalid_kml_document(self):
     "Tests the parsing of an invalid KML document"
     fileobject = StringIO('<bad_element />')
     try:
         tree = parse(fileobject, schema=Schema("ogckml22.xsd"))
         self.assertTrue(False)
     except etree.XMLSyntaxError:
         self.assertTrue(True)
     except:
         self.assertTrue(False)
开发者ID:giricgoyal,项目名称:CS526_UIC_Fall2013,代码行数:10,代码来源:test_parser.py


示例18: load_kml_file

def load_kml_file(filename, schema_type = "kml22gx.xsd"):
    with open(filename) as fh:
        schema_gx = Schema(schema_type)
        doc = parser.parse(fh)
        
        if (schema_gx.validate(doc)):
            return doc.getroot()
        else:
            print "Invalid kml format for file: "+str(filename)
            return None
开发者ID:wmaciel,项目名称:van-crime,代码行数:10,代码来源:utilities.py


示例19: test_write_python_script_for_kml_document_with_comments

 def test_write_python_script_for_kml_document_with_comments(self):
     """Tests the creation of an OGC KML document with several comments"""
     import os
     import tempfile
     from pykml.parser import parse
     from pykml.factory import write_python_script_for_kml_document
     
     test_datafile = path.join(
         path.dirname(__file__),
         'testfiles',
         'simple_file_with_comments.kml'
     )
     schema = Schema('kml22gx.xsd')
     with open(test_datafile) as f:
         doc = parse(f, schema=schema)
     script = write_python_script_for_kml_document(doc)
     
     # create a temporary python file
     handle, tfile = tempfile.mkstemp(suffix='.py')
     #print tfile  # Useful for debugging
     with open(tfile, 'w') as f:
         f.write(script)
     
     # execute the temporary python file to create a KML file
     import subprocess
     current_env = os.environ.copy()
     current_env["PYTHONPATH"] = os.path.abspath(os.path.join(os.getcwd(),'..'))
     handle, temp_kml_file = tempfile.mkstemp(suffix='.kml')
     #print temp_kml_file  # Useful for debugging
     with open(temp_kml_file, 'w') as f:
         exit_code = subprocess.call(["python",tfile], stdout=f, env=current_env)
     self.assertEqual(exit_code, 0)
     
     # parse and validate the KML generated by the temporary script
     doc2 = parse(temp_kml_file, schema=schema)
     # test that the root element is as expected
     self.assertEqual(doc2.docinfo.root_name, 'kml')
     
     self.assertEqual(etree.tostring(doc), etree.tostring(doc2))
     
     #import ipdb; ipdb.set_trace()
     pass
开发者ID:giricgoyal,项目名称:CS526_UIC_Fall2013,代码行数:42,代码来源:test_factory.py


示例20: parse

 def parse(self):
     available = 0
     traveling = 0
     live = self.vcub()
     with open(current_dir + '/doc.kml') as f:
         doc = parser.parse(f)
     placemarks = []
     for placemark in doc.getroot().Document.Folder.Placemark:
         coordinates = str(placemark.Point.coordinates).split(',')
         coordinates = [coordinates[1], coordinates[0]]
         data = {}
         for child in placemark.ExtendedData.SchemaData.SimpleData:
             data[child.get('name')] = child.text
         try:
             station = live[data['NOM']]
             for key in station.keys():
                 data[key] = station[key]
             if data['etat'] == 'DECONNECTEE':
                 data['class'] = 'offline'
             else:
                 if int(data['nbvelos']) == 0:
                     data['class'] = 'velos_red'
                     if int(data['nbplaces']) == 0:
                         data['class'] += ' places_red'
                     if int(data['nbplaces']) <= 3:
                         data['class'] += ' places_orange'
                     if int(data['nbplaces']) > 3:
                         data['class'] += ' places_green'
                 elif int(data['nbvelos']) <= 3:
                     data['class'] = 'velos_orange'
                     if int(data['nbplaces']) == 0:
                         data['class'] += ' places_red'
                     if int(data['nbplaces']) <= 3:
                         data['class'] += ' places_orange'
                     if int(data['nbplaces']) > 3:
                         data['class'] += ' places_green'
                 elif int(data['nbvelos']) > 3:
                     data['class'] = 'velos_green'
                     if int(data['nbplaces']) == 0:
                         data['class'] += ' places_red'
                     if int(data['nbplaces']) <= 3:
                         data['class'] += ' places_orange'
                     if int(data['nbplaces']) > 3:
                         data['class'] += ' places_green'
         except:
             data['class'] = 'offline'
         try:
             available += int(data['nbvelos'])
             traveling += int(data['NBSUPPOR']) - int(data['nbplaces'])
         except:
             pass
         placemarks.append({'coordinates': coordinates, 'data': data})
     self.data_vcub = {'available': available, 'traveling': traveling, 'stations': sorted(placemarks, key=lambda k: k['data']['NOM']), 'updated': placemarks[0]['data']['heure']}
     return self.data_vcub
开发者ID:adrien-f,项目名称:vcub_live,代码行数:54,代码来源:__init__.py



注:本文中的pykml.parser.parse函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python music.NoteSeq类代码示例发布时间:2022-05-25
下一篇:
Python parser.fromstring函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap