2

I want to convert this XML file to a polyline shapefile.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ci nc="BMSKDMA10" nl="BMskDMA10" mt="6" ct="DM" dtt="17342" dtu="15/01/2016 09:47:29"> 
  <tr la="33.5510900" lo="-7.5741300" tr="COLLECTE" nr="BOULEVARD ACHAHID LAARBI AL BANNAY" ce="CASABLANCA" er="0" mt="6" cm="3" id="0" dt="418" />
  <tr la="33.5532900" lo="-7.5704600" tr="COLLECTE" nr="BOULEVARD ACHAHID LAARBI AL BANNAY" ce="CASABLANCA" er="0" mt="6" cm="2" id="1" dt="206" />
  <tr la="33.5547700" lo="-7.5714700" tr="COLLECTE" nr="BOULEVARD AL JOULANE" ce="CASABLANCA" er="0" mt="6" cm="3" sa="" hc="" id="2" dt="182" />
</ci>

I tried this code with pyshp library, it works as points when I put:

w = shapefile.Writer(shapefile.POINT)

and when I put :

w = shapefile.Writer(shapefile.POLYLINE)

I do not get a polyline

from xml.etree import ElementTree
import shapefile
import os

xml_file = 'BMSKDMA10.xml'
shape_file = 'BMSKDMA10.shp'
projection = 'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'

tree = ElementTree.parse(xml_file)
w = shapefile.Writer(shapefile.POLYLINE)

# create fields
w.field('la', fieldType = 'N', size = 200)
w.field('lo', fieldType = 'N', size = 200)
w.field('tr', fieldType = 'C', size = 200)
w.field('nr', fieldType = 'C', size = 200)
w.field('ce', fieldType = 'C', size = 200)
w.field('er', fieldType = 'N', size = 200)
w.field('mt', fieldType = 'N', size = 200)
w.field('cm', fieldType = 'N', size = 200)
w.field('id', fieldType = 'N', size = 200)
w.field('dt', fieldType = 'N', size = 200)

root = tree.getroot()
ci = root.getchildren()

for tr in ci:

    part = []
    part.append([float(tr.get('lo')), float(tr.get('la'))])
    w.poly(parts=[part])

    # copy attributes
    w.record(float(tr.get('la')),float(tr.get('lo')), tr.get('tr'),tr.get('nr'),tr.get('ce'),int(tr.get('er')),int(tr.get('mt')),int(tr.get('cm')),int(tr.get('id')),int(tr.get('dt')))
w.save(shape_file)

# create the PRJ file
with open(os.path.splitext(shape_file)[0] + os.extsep + 'prj', 'w') as prj:prj.write(projection)

Can you help me to resolve this problem?

3
  • 1
    What have you researched/tried so far? I got numerous hits from Google using google.com/search?q=xml+shapefile+conversion
    – PolyGeo
    Commented Feb 6, 2016 at 2:49
  • 2
    Also have you tried searching this very site? gis.stackexchange.com/search?q=xml+to+shapefile+convert
    – raphael
    Commented Feb 6, 2016 at 5:02
  • I know very little about pyshp but I do think there's a problem with having w.poly(parts=[part]) in the for, and the writing of attributes too. You only have one feature (one line with three vertices) so can't write three sets of attributes.
    – Midavalo
    Commented Feb 6, 2016 at 21:09

1 Answer 1

-1

.xml file you can open through qgis and export into shape file.

2
  • 4
    Hi! Welcome to GIS SE! Could you please add more information to your answer or provide more context on how to achieve this in QGIS. By doing so, others can reproduce the worksteps more easily.
    – root676
    Commented Aug 16, 2019 at 9:20
  • Can't see how this XML file can be added in QGIS, how did you do it?
    – nmtoken
    Commented Mar 8, 2022 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.