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?

0

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

  • 3
    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 Aug 16 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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