I have issue with PostGIS, Excel spreadsheets and Python OGR bindings. I need to read many Excel spreadsheets (with same schema), parse latitude and longitude fields, create points, re-project them and then write final rows to PostGIS. Simple. I create following script to do this (only important part of script):

count = 0
for f in file_queue:
    input_ds = input_drv.Open(f)      
    in_lyr = input_ds.GetLayerByName(input_layer)

    #first pass - create target table and fields...
    if count == 0:
        out_lyr = out_ds.CreateLayer(output_layer, output_sr, ogr.wkbPoint, ['OVERWRITE=YES'])
        for x in xrange(in_lyr.GetLayerDefn().GetFieldCount()):

        for x in xrange(in_lyr.GetFeatureCount()):
            tmp_feature = in_lyr.GetNextFeature()
            x_coord = tmp_feature.GetField(in_lyr.GetLayerDefn().GetFieldIndex('X'))
            y_coord = tmp_feature.GetField(in_lyr.GetLayerDefn().GetFieldIndex('Y'))
            if x_coord and y_coord:
                x_coord = parse_dms(x_coord, input_dms_format_regex)
                y_coord = parse_dms(y_coord, input_dms_format_regex)
                source_geom = create_point(x_coord, y_coord)
                projected_geom = reproject_geometry(source_geom, input_sr, output_sr)

    count += 1

But... when i run this script, no geometry is written to PostGIS - geometry field is present in table but is always null. Although when in step thorough the code in debug mode, all variables are correctly initialized and filled. FIY reproject_geometry function returns Clone of passed source geometry.

There is create_point function:

def create_point(x, y):
    if x and y:
       geom = ogr.Geometry(ogr.wkbPoint) 
       geom.AddPoint_2D(x, y)
    return  geom.Clone()

Any advice?

  • Not sure, but did you try to use the geometry as WKT? WKB should work of course, but could be worth a try. – Matte Oct 2 '16 at 17:11
  • Thanks for advice, but that is not the issue... I thing, the real reason is that my temp_feature is copy of input feature which in turn is excel row. So i am setting geometry on feature without geometry column... I try to fix it tomorrow (i fact late today, because there is 0:20 AM). Then i let now how it goes... – DavidP Oct 2 '16 at 22:25

Your Answer

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

Browse other questions tagged or ask your own question.