readOGR错过了ExtendedData

时间:2017-04-24 20:45:04

标签: r kml rgdal

以下是我试图用readOGR读取的KML的摘录。 <ExtendedData>内容被遗漏:

test.kml

<?xml version='1.0' encoding='utf-8'?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Schema name="election-presidentielle-2017" id="election-presidentielle-2017">
      <SimpleField name="codreg" type="string">
        <displayName>Code Région</displayName>
      </SimpleField>
      <SimpleField name="codreg3car" type="string">
        <displayName>Code Région 3</displayName>
      </SimpleField>
      <SimpleField name="libreg" type="string">
        <displayName>Région</displayName>
      </SimpleField>
      <SimpleField name="coddpt" type="string">
        <displayName>Code Département</displayName>
      </SimpleField>
      <SimpleField name="codmindpt" type="string">
        <displayName>Code Département
        </displayName>
      </SimpleField>
      <SimpleField name="coddpt3car" type="string">
        <displayName>Code Département 3</displayName>
      </SimpleField>
      <SimpleField name="libdpt" type="string">
        <displayName>Département</displayName>
      </SimpleField>
      <SimpleField name="numtour" type="string">
        <displayName>Tour</displayName>
      </SimpleField>
      <SimpleField name="codsubcom" type="string">
        <displayName>Code Commune</displayName>
      </SimpleField>
      <SimpleField name="libsubcom" type="string">
        <displayName>Commune</displayName>
      </SimpleField>
      <SimpleField name="nompsn" type="string">
        <displayName>Nom Candidat</displayName>
      </SimpleField>
      <SimpleField name="prenompsn" type="string">
        <displayName>Prénom Candidat</displayName>
      </SimpleField>
      <SimpleField name="civilitepsn" type="string">
        <displayName>Civilité Candidat</displayName>
      </SimpleField>
      <SimpleField name="numpanneaucand" type="string">
        <displayName>Numéro Panneau Candidat</displayName>
      </SimpleField>
      <SimpleField name="nbvoix" type="int">
        <displayName>Nombre de voix</displayName>
      </SimpleField>
      <SimpleField name="rapportinscrit" type="double">
        <displayName>Rapport Inscrit</displayName>
      </SimpleField>
      <SimpleField name="rapportexprime" type="double">
        <displayName>rapportexprime</displayName>
      </SimpleField>
      <SimpleField name="code_insee" type="string">
        <displayName>Code Insee</displayName>
      </SimpleField>
    </Schema>
    <Placemark>
      <name>84</name>
      <Polygon>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>4.9030768998,46.1579771394 4.903773126,46.157645709 4.9067820906,46.1562053906 4.9085001702,46.1548830643 4.9095492036,46.1537791314 4.9106569632,46.1525390878 4.9112674797,46.1517595766 4.9161461217,46.1453469021
              4.9160777584,46.1439086534 4.9156193663,46.1424337477 4.9145874913,46.1404541802 4.9093076435,46.1307852326 4.9093613658,46.1291638781 4.9158884782,46.127574897 4.9200298575,46.1257142424 4.9230135966,46.1235415672 4.9258961462,46.1201896347
              4.9262770895,46.120045356 4.9266095705,46.120218064 4.9315756331,46.1225684278 4.9367339062,46.1262421207 4.9378762749,46.127116182 4.9390381372,46.1283943404 4.9395243837,46.1291030402 4.9399472307,46.1298137221 4.940252746,46.1307966353
              4.9406425153,46.1321834498 4.9409012267,46.1335274654 4.9409959919,46.1341554766 4.940503709,46.134707048 4.9398299924,46.1395835589 4.9406706269,46.1415896 4.9428982044,46.1455436813 4.9460544153,46.1492521236 4.9463858083,46.1494238953
              4.9469685666,46.1494112058 4.948566391,46.1489685562 4.9578258548,46.1529852741 4.9582913471,46.1532447772 4.9553500484,46.1540331688 4.9508770382,46.1553071858 4.9453021789,46.1573520303 4.9431568487,46.1584102059 4.9401772735,46.1602923709
              4.9368787943,46.1618862443 4.9337395419,46.1626333003 4.931584178,46.1622772346 4.9312514883,46.1639105549 4.9316886461,46.1648390082 4.9330032704,46.167600886 4.9351734924,46.1696212725 4.9366153847,46.1699939409 4.9376185464,46.1706452011
              4.9387676797,46.1730044996 4.939996705,46.1756767881 4.9400915327,46.1763047983 4.9397434505,46.1771682606 4.9383177085,46.1785058892 4.9369448895,46.1796183085 4.9359306904,46.1800913711 4.9341221355,46.1815732794 4.9330127333,46.1827685385
              4.93289657,46.1830416412 4.9287600537,46.1832713453 4.916470741,46.1837323363 4.9147078505,46.183457594 4.9138535896,46.1832071613 4.913320385,46.1829035785 4.9095372185,46.1796146164 4.9088536889,46.1788649666 4.9074867671,46.176734229
              4.907292177,46.1690883427 4.905691265,46.1646916296 4.9045896831,46.1612287097 4.9045764202,46.1609587077 4.9042950093,46.1604698149 4.903096103,46.1583812534 4.9030768998,46.1579771394
            </coordinates>
          </LinearRing>
        </outerBoundaryIs>
      </Polygon>
      <ExtendedData>
        <SchemaData schemaUrl="election-presidentielle-2017">
          <SimpleData name="codreg">84</SimpleData>
          <SimpleData name="codreg3car">084</SimpleData>
          <SimpleData name="libreg">Auvergne et Rhône-Alpes</SimpleData>
          <SimpleData name="coddpt">01</SimpleData>
          <SimpleData name="codmindpt">01</SimpleData>
          <SimpleData name="coddpt3car">001</SimpleData>
          <SimpleData name="libdpt">Ain</SimpleData>
          <SimpleData name="numtour">1</SimpleData>
          <SimpleData name="codsubcom">001</SimpleData>
          <SimpleData name="libsubcom">L'Abergement-Clémenciat</SimpleData>
          <SimpleData name="nompsn">DUPONT-AIGNAN</SimpleData>
          <SimpleData name="prenompsn">Nicolas</SimpleData>
          <SimpleData name="civilitepsn">M.</SimpleData>
          <SimpleData name="numpanneaucand">1</SimpleData>
          <SimpleData name="nbvoix">34</SimpleData>
          <SimpleData name="rapportinscrit">5.69</SimpleData>
          <SimpleData name="rapportexprime">6.87</SimpleData>
          <SimpleData name="code_insee">01001</SimpleData>
        </SchemaData>
      </ExtendedData>
    </Placemark>
    <Placemark>
      <name>84</name>
      <Polygon>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>4.9030768998,46.1579771394 4.903773126,46.157645709 4.9067820906,46.1562053906 4.9085001702,46.1548830643 4.9095492036,46.1537791314 4.9106569632,46.1525390878 4.9112674797,46.1517595766 4.9161461217,46.1453469021
              4.9160777584,46.1439086534 4.9156193663,46.1424337477 4.9145874913,46.1404541802 4.9093076435,46.1307852326 4.9093613658,46.1291638781 4.9158884782,46.127574897 4.9200298575,46.1257142424 4.9230135966,46.1235415672 4.9258961462,46.1201896347
              4.9262770895,46.120045356 4.9266095705,46.120218064 4.9315756331,46.1225684278 4.9367339062,46.1262421207 4.9378762749,46.127116182 4.9390381372,46.1283943404 4.9395243837,46.1291030402 4.9399472307,46.1298137221 4.940252746,46.1307966353
              4.9406425153,46.1321834498 4.9409012267,46.1335274654 4.9409959919,46.1341554766 4.940503709,46.134707048 4.9398299924,46.1395835589 4.9406706269,46.1415896 4.9428982044,46.1455436813 4.9460544153,46.1492521236 4.9463858083,46.1494238953
              4.9469685666,46.1494112058 4.948566391,46.1489685562 4.9578258548,46.1529852741 4.9582913471,46.1532447772 4.9553500484,46.1540331688 4.9508770382,46.1553071858 4.9453021789,46.1573520303 4.9431568487,46.1584102059 4.9401772735,46.1602923709
              4.9368787943,46.1618862443 4.9337395419,46.1626333003 4.931584178,46.1622772346 4.9312514883,46.1639105549 4.9316886461,46.1648390082 4.9330032704,46.167600886 4.9351734924,46.1696212725 4.9366153847,46.1699939409 4.9376185464,46.1706452011
              4.9387676797,46.1730044996 4.939996705,46.1756767881 4.9400915327,46.1763047983 4.9397434505,46.1771682606 4.9383177085,46.1785058892 4.9369448895,46.1796183085 4.9359306904,46.1800913711 4.9341221355,46.1815732794 4.9330127333,46.1827685385
              4.93289657,46.1830416412 4.9287600537,46.1832713453 4.916470741,46.1837323363 4.9147078505,46.183457594 4.9138535896,46.1832071613 4.913320385,46.1829035785 4.9095372185,46.1796146164 4.9088536889,46.1788649666 4.9074867671,46.176734229
              4.907292177,46.1690883427 4.905691265,46.1646916296 4.9045896831,46.1612287097 4.9045764202,46.1609587077 4.9042950093,46.1604698149 4.903096103,46.1583812534 4.9030768998,46.1579771394
            </coordinates>
          </LinearRing>
        </outerBoundaryIs>
      </Polygon>
      <ExtendedData>
        <SchemaData schemaUrl="election-presidentielle-2017">
          <SimpleData name="codreg">84</SimpleData>
          <SimpleData name="codreg3car">084</SimpleData>
          <SimpleData name="libreg">Auvergne et Rhône-Alpes</SimpleData>
          <SimpleData name="coddpt">01</SimpleData>
          <SimpleData name="codmindpt">01</SimpleData>
          <SimpleData name="coddpt3car">001</SimpleData>
          <SimpleData name="libdpt">Ain</SimpleData>
          <SimpleData name="numtour">1</SimpleData>
          <SimpleData name="codsubcom">001</SimpleData>
          <SimpleData name="libsubcom">L'Abergement-Clémenciat</SimpleData>
          <SimpleData name="nompsn">LE PEN</SimpleData>
          <SimpleData name="prenompsn">Marine</SimpleData>
          <SimpleData name="civilitepsn">Mme</SimpleData>
          <SimpleData name="numpanneaucand">2</SimpleData>
          <SimpleData name="nbvoix">126</SimpleData>
          <SimpleData name="rapportinscrit">21.07</SimpleData>
          <SimpleData name="rapportexprime">25.45</SimpleData>
          <SimpleData name="code_insee">01001</SimpleData>
        </SchemaData>
      </ExtendedData>
    </Placemark>
  </Document>
</kml>

和..

require(rgdal)
x = readOGR('test.kml', layer=ogrListLayers("test.kml"), verbose=T)
#> OGR data source with driver: KML 
#> Source: "test.kml", layer: "Layer #0"
#> with 2 features
#> It has 2 fields
x@data
#>   Name Description
#> 0   84            
#> 1   84

Not a new problem但我找不到解决方案。有什么建议?如果readOGR无法管理它,我想知道是否有一种方法可以将<ExtendedData>(可能在R之外)重新格式化为可读标记?

1 个答案:

答案 0 :(得分:0)

确定此node.js工作流将<ExtendedData>内容提取到csv(以及其他内容):

ogr2ogr -f CSV test.csv test.kml

..你可以与R中的空间数据重新组合,例如

spdf@data = read.csv('test.csv')