0

Context:


I use lxml library that generates some dictionary (infact it is xml attribute dictionary) from xml and i am trying to write these into a shapefile-i.e. get one element-attribute from xml and write it into shapefile (using below function) then get next xml element-attribute and write it into shapefile and so on..

Below is the function that takes some parameters and writes point or polyline geometry based on passed parameters.

from shapely.geometry import Point, LineString
import fiona
from fiona.crs import from_epsg

def single_geom_writer(ls,att_dict,feat_type):
    #Create geometry when called
    def geom_setter(ls):
        if len(*ls)==2:
            my_geom = Point(ls)
        elif len(*ls)>2:
            my_geom = LineString(ls)
        else:
            raise Exception("Can not handle writing geometry")
        return my_geom

    # Define a schema when called
    def schema_setter(feat_type):
        if feat_type=='lane':
            sch = {
                'geometry': geom_setter(ls).geom_type,
                'properties':{'id': 'str','index':'str','speed':'str','length':'str','width':'str'},
                  }
        elif feat_type=='junction':
            sch = {
                'geometry': geom_setter(ls).geom_type,
                'properties': {'intLanes': 'str', 'junction_shape': 'str', 'incLanes': 'str', 'y': 'str', 'x': 'str', 'type': 'str', 'id': 'str'},
                  }

        else:
            raise Exception("Schema error! Check the passed schema with collection schema")
        return sch
    #Create schema and geometry dynamically from passed dparameters
    my_schema = schema_setter(feat_type)
    my_geom = geom_setter(ls)
    wr_dict = {'geometry': mapping(my_geom),'properties': att_dict} 
    with fiona.open(path="TestShapefile\my_shp_clean1.shp", mode='w', driver='ESRI Shapefile', schema=my_schema,crs =from_epsg(32632)) as output:
        output.write(wr_dict)

Function parameters explanation- e.g.point writing case

ls = [(488901.97760411515, 5078198.250875515)]
att_dict = {'intLanes': ':A_0_0 :A_1_0', 'junction_shape': '30.62,42.35 33.90,36.62 28.77,40.78', 'incLanes': 'B-A_0 A0-A_0', 'y': '39.18', 'x': '31.72', 'type': 'unregulated', 'id': 'A'}
feat_type = 'junction'

And my_schema is as below.

my_schema = {'geometry': 'Point', 'properties': {'intLanes': 'str', 'y': 'str', 'incLanes': 'str', 'junction_shape': 'str', 'x': 'str', 'type': 'str', 'id': 'str'}}

But without no success, it creates shapefile but i think it is corrupt since can not open in arcmap but shown error like could not open the specified file

What i want:

Function single_geom_writer will take three parameters
1) list of geometry(ls)
2) shapefile attribute dictionary(att_dict)
3) a flag(feat_type) to control to be written shapefile if it will be polyline or point.

The single_geom_writer will automatically determine if the passed argument(ls) is point or polyline and will create shapely geometry object.Passed att_dict is just attribute of shapefile that will match following selected schema . Based on passed feat_type this function will set schema which will match previuos attribute dictionary.


Problem:


This code-

with fiona.open(path="TestShapefile\my_shp_clean1.shp", mode='w', driver='ESRI Shapefile', schema=my_schema,crs =from_epsg(32632)) as output:
    output.write(wr_dict)

is not writing geometry even no exception is being raised- created shapefile is blank.

Edit:

See working and not working code-

#Working
import fiona
from fiona.crs import from_epsg
my_schema={'geometry': 'Point', 'properties': {'index': 'str', 'length': 'str', 'speed': 'str', 'id': 'str', 'width': 'str'}}
wr_dict = {'geometry': {'type': 'Point', 'coordinates': (488901.97760411515, 5078198.250875515)}, 'properties': {'index': '0', 'length': '49.79', 'speed': '13.89', 'id': 'A-A0_0', 'width': '3.25'}}
with fiona.open("my_shp_clean121.shp", mode='w', driver='ESRI Shapefile', schema=my_schema,crs =from_epsg(32632)) as output:
    output.write(wr_dict)

#Not working
import fiona
from fiona.crs import from_epsg
my_schema={'geometry': 'Point', 'properties': {'intLanes': 'str', 'y': 'str', 'incLanes': 'str', 'junction_shape': 'str', 'x': 'str', 'type': 'str', 'id': 'str'}}
wr_dict = {'geometry': {'type': 'Point', 'coordinates': (488901.97760411515, 5078198.250875515)}, 'properties': {'intLanes': ':A_0_0 :A_1_0', 'junction_shape': '30.62,42.35 33.90,36.62 28.77,40.78', 'incLanes': 'B-A_0 A0-A_0', 'y': '39.18', 'x': '31.72', 'type': 'unregulated', 'id': 'A'}}
with fiona.open("my_shp_clean12221.shp", mode='w', driver='ESRI Shapefile', schema=my_schema,crs =from_epsg(32632)) as output:
    output.write(wr_dict)

EDIT: QGIS opens the created shapefile but it is blank! Any help is welcome.

closed as off-topic by Vince, PolyGeo Nov 14 '15 at 22:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – Vince, PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    It's very difficult to tell from what you have posted, but if you are generating shapefiles with more than one topology class, then the files are not legal. Please edit your question to use a MCVE, containing only the code needed to demonstrate the behavior, with a complete description of inputs and a hexadecimal dump (od -t x1) of the single-feature output files. – Vince Nov 14 '15 at 16:06
  • 1
    What is wr_dict ? – gene Nov 14 '15 at 16:57
  • @Vince Should i post the full code? – SIslam Nov 14 '15 at 17:11
  • 1
    Not more code, less code. The minimum code required to exhibit the behavior. – Vince Nov 14 '15 at 17:43
  • 1
    Try to hardwire it to something that you think/know should work, and then compare that to what is being written by that other non-GIS code from the "lxml library that generates some dictionary". If you can isolate it to fiona (GIS/spatial library) and isolate it from lxml (IT/XML library - better researched at Stack Overflow) then you have something that is on-topic for here. – PolyGeo Nov 15 '15 at 5:26
2

Writing the shapefile

import fiona
from fiona.crs import from_epsg
my_schema={'geometry': 'Point', 'properties': {'intLanes': 'str', 'y': 'str', 'incLanes': 'str', 'junction_shape': 'str', 'x': 'str', 'type': 'str', 'id': 'str'}}
wr_dict = {'geometry': {'type': 'Point', 'coordinates': (488901.97760411515, 5078198.250875515)}, 'properties': {'intLanes': ':A_0_0 :A_1_0', 'junction_shape': '30.62,42.35 33.90,36.62 28.77,40.78', 'incLanes': 'B-A_0 A0-A_0', 'y': '39.18', 'x': '31.72', 'type': 'unregulated', 'id': 'A'}}
with fiona.open("my_shp_clean1.shp", mode='w', driver='ESRI Shapefile', schema=my_schema,crs =from_epsg(32632)) as output:
     output.write(wr_dict)

Opening the resulting shapefile

import fiona
c = fiona.open("my_shp_clean1.shp")
c.schema
{'geometry': 'Point', 'properties': OrderedDict([(u'intLanes', 'str:80'), (u'y', 'str:80'), (u'incLanes', 'str:80'), (u'junction_s', 'str:80'), (u'x', 'str:80'), (u'type', 'str:80'), (u'id', 'str:80')])}
c.crs
{'init': u'epsg:32632'}
c.next()
{'geometry': {'type': 'Point', 'coordinates': (488901.97760411515, 5078198.250875515)}, 'type': 'Feature', 'id': '0', 'properties': OrderedDict([(u'intLanes', u':A_0_0 :A_1_0'), (u'y', u'39.18'), (u'incLanes', u'B-A_0 A0-A_0'), (u'junction_s', u'30.62,42.35 33.90,36.62 28.77,40.78'), (u'x', u'31.72'), (u'type', u'unregulated'), (u'id', u'A')])}

The result seems ok

Secondly, when I open the resulting shapefile with QGIS, there is apparently no problem:

enter image description here

  • look at my script to create the shapefile with your resulting data – gene Nov 14 '15 at 17:56
  • Ah! i am getting blank shapefile even when i run simple-code your i.e first code.What happened – SIslam Nov 14 '15 at 18:11
  • 1
    Therefore, the problem is your Fiona installation – gene Nov 14 '15 at 18:18
  • Yeah! i just tested fiona is so healthy and works superb with other code. – SIslam Nov 15 '15 at 5:08
  • does 'junction_shape' automatically changed to 'junction_s'?In my case whole process gets corrupted and blank file gets created. – SIslam Nov 15 '15 at 11:28

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