Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-31 Thread Tamas Szekeres
I think it wouldn't be a big deal extending OGR SQL to accept constant
values in the select list.
In this case the parser would place the constant value into the
field_name memeber of swq_col_def, and field_index = -1 would denote
that it should be treated as a constant
in OGRGenSQLResultsLayer::TranslateFeature when copying the features over.

Best regards,

Tamas



2008/10/29 Tyler Erickson <[EMAIL PROTECTED]>:
>
> I am interested in approaches for adding a populated field to a shapefile
> (for example, adding a new field named 'source_url' with the value
> 'http://somewebsite.com').  I would like to do this for several thousand
> files.
>
> At first I thought that I might be able to accomplish it using ogr2org with
> a sql clause, such as:
>
> ogr2ogr -sql "select *, 'http://somewebsite.com' as source_url from infile"
> outfile.shp infile.shp
>
> but that didn't work since ogr2ogr supports a limited set of SQL, described
> at:
> http://www.gdal.org/ogr/ogr_sql.html
>
> Any ideas on how to accomplish this? (I would prefer suggestions that can be
> scripted with python.)
>
> - Tyler
> --
> View this message in context: 
> http://n2.nabble.com/methods-for-programatically-adding-fields-to-shapefiles-tp1395535p1395535.html
> Sent from the OSGeo Discuss mailing list archive at Nabble.com.
>
> ___
> Discuss mailing list
> Discuss@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/discuss
>
___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-31 Thread Tim Sutton
Hi Frank



2008/10/31 Frank Warmerdam <[EMAIL PROTECTED]>:
> Tim Sutton wrote:
>>
>> Hi
>>
>> Are there any suggestions on how to do this from c++? Something we've
>> long wanted to add support for in QGIS.
>>
>> Frank Warmerdam was reticent about adding it to ogr since it would
>> have many crosscutting implications...
>
> Tim,
>
> I would note that I would not mind implementing OGRLayer::CreateField()
> for shapefiles that already have features (though I'm not promising
> I have time).  I think I was wanting to avoid more general schema
> alteration (removing fields, reordering, changing names, etc).

That would be great if you have time - no rush though since we are in
feature freeze and we would have to wait for your changes to get into
a gdal release and then into distros etc..


Regards

Tim

>
> Best regards,
> --
> ---+--
> I set the clouds in motion - turn up   | Frank Warmerdam,
> [EMAIL PROTECTED]
> light and sound - activate the windows | http://pobox.com/~warmerdam
> and watch the world go round - Rush| Geospatial Programmer for Rent
>
> ___
> Discuss mailing list
> Discuss@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/discuss
>



-- 
Tim Sutton - QGIS Project Steering Committee Member (Release  Manager)
==
Visit http://linfiniti.com to find out about:
 * QGIS programming services
 * Mapserver and PostGIS based hosting plans
 * FOSS Consulting Services
Skype: timlinux Irc: timlinux on #qgis at freenode.net
==
___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-31 Thread Frank Warmerdam

Tim Sutton wrote:

Hi

Are there any suggestions on how to do this from c++? Something we've
long wanted to add support for in QGIS.

Frank Warmerdam was reticent about adding it to ogr since it would
have many crosscutting implications...


Tim,

I would note that I would not mind implementing OGRLayer::CreateField()
for shapefiles that already have features (though I'm not promising
I have time).  I think I was wanting to avoid more general schema
alteration (removing fields, reordering, changing names, etc).

Best regards,
--
---+--
I set the clouds in motion - turn up   | Frank Warmerdam, [EMAIL PROTECTED]
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush| Geospatial Programmer for Rent

___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-31 Thread Tim Sutton
Hi

Are there any suggestions on how to do this from c++? Something we've
long wanted to add support for in QGIS.

Frank Warmerdam was reticent about adding it to ogr since it would
have many crosscutting implications...

Regards

Tim

2008/10/31 Tyler Erickson <[EMAIL PROTECTED]>:
>
> Thanks for all the suggestions.  Although it is still a work in progress,
> it's looking like the ogr library approach suggested by David will work out
> for me...
>
> - Tyler
>
>
> def create_formatted_shapefile_using_ogr(infile, outfile):
>
># setup a spatial reference
>srs_out = osr.SpatialReference()
>srs_out.SetFromUserInput('WGS84')
>
>ogrdriver = ogr.GetDriverByName('ESRI Shapefile')
>
>dsInput = ogrdriver.Open(infile)
>mLayer = dsInput.GetLayer()
>featureCount = mLayer.GetFeatureCount()
>
># create the output shapefile
>if os.path.exists(outfile):
>ogrdriver.DeleteDataSource(outfile)
>dsOutput = ogrdriver.CreateDataSource(outfile)
>outLayer = dsOutput.CreateLayer(dsOutput.GetName(), \
>geom_type = ogr.wkbPolygon, \
>srs = srs_out)
>outLayer.CreateField(ogr.FieldDefn('start_date', ogr.OFTDate))
>outLayer.CreateField(ogr.FieldDefn('fire_code', ogr.OFTString))
>outLayer.CreateField(ogr.FieldDefn('fire_name', ogr.OFTString))
>outLayer.CreateField(ogr.FieldDefn('source', ogr.OFTString))
>
>print 'FeatureCount = ' + str(featureCount)
>
>for iFeature in range(0,mLayer.GetFeatureCount()):
>inFeature = mLayer.GetFeature(iFeature)
>
>geom = inFeature.GetGeometryRef()
>start_date = inFeature.GetFieldAsString('year') + "-" + \
>inFeature.GetFieldAsString('startmonth') + "-" + \
>inFeature.GetFieldAsString('startday')
>
># reproject to WGS84
>geom.TransformTo(srs_out)
>
># write the attributes to the output file
>outFeature = ogr.Feature(feature_def=outLayer.GetLayerDefn())
>outFeature.SetGeometry(geom)
>outFeature.SetFID(1)
>outFeature.SetField('fire_code', inFeature.GetField('fire_id'))
>outFeature.SetField('fire_name', inFeature.GetField('fire_name'))
>outFeature.SetField('source', infile)
>outLayer.CreateFeature(outFeature)
>
>outFeature.Destroy()
>inFeature.Destroy()
>
>dsInput.Destroy()
>dsOutput.Destroy()
>
>
> --
> View this message in context: 
> http://n2.nabble.com/methods-for-programatically-adding-fields-to-shapefiles-tp1395535p1437802.html
> Sent from the OSGeo Discuss mailing list archive at Nabble.com.
>
> ___
> Discuss mailing list
> Discuss@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/discuss
>



-- 
Tim Sutton - QGIS Project Steering Committee Member (Release  Manager)
==
Visit http://linfiniti.com to find out about:
 * QGIS programming services
 * Mapserver and PostGIS based hosting plans
 * FOSS Consulting Services
Skype: timlinux Irc: timlinux on #qgis at freenode.net
==
___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-31 Thread Tyler Erickson

Thanks for all the suggestions.  Although it is still a work in progress,
it's looking like the ogr library approach suggested by David will work out
for me...

- Tyler


def create_formatted_shapefile_using_ogr(infile, outfile):

# setup a spatial reference
srs_out = osr.SpatialReference()
srs_out.SetFromUserInput('WGS84')

ogrdriver = ogr.GetDriverByName('ESRI Shapefile')

dsInput = ogrdriver.Open(infile)
mLayer = dsInput.GetLayer()
featureCount = mLayer.GetFeatureCount()

# create the output shapefile
if os.path.exists(outfile):
ogrdriver.DeleteDataSource(outfile)
dsOutput = ogrdriver.CreateDataSource(outfile)
outLayer = dsOutput.CreateLayer(dsOutput.GetName(), \
geom_type = ogr.wkbPolygon, \
srs = srs_out)
outLayer.CreateField(ogr.FieldDefn('start_date', ogr.OFTDate))
outLayer.CreateField(ogr.FieldDefn('fire_code', ogr.OFTString))
outLayer.CreateField(ogr.FieldDefn('fire_name', ogr.OFTString))
outLayer.CreateField(ogr.FieldDefn('source', ogr.OFTString))

print 'FeatureCount = ' + str(featureCount)

for iFeature in range(0,mLayer.GetFeatureCount()):
inFeature = mLayer.GetFeature(iFeature)

geom = inFeature.GetGeometryRef()
start_date = inFeature.GetFieldAsString('year') + "-" + \
inFeature.GetFieldAsString('startmonth') + "-" + \
inFeature.GetFieldAsString('startday')

# reproject to WGS84
geom.TransformTo(srs_out)

# write the attributes to the output file
outFeature = ogr.Feature(feature_def=outLayer.GetLayerDefn())
outFeature.SetGeometry(geom)
outFeature.SetFID(1)
outFeature.SetField('fire_code', inFeature.GetField('fire_id'))
outFeature.SetField('fire_name', inFeature.GetField('fire_name'))
outFeature.SetField('source', infile)
outLayer.CreateFeature(outFeature)

outFeature.Destroy()
inFeature.Destroy()

dsInput.Destroy()
dsOutput.Destroy()
 

-- 
View this message in context: 
http://n2.nabble.com/methods-for-programatically-adding-fields-to-shapefiles-tp1395535p1437802.html
Sent from the OSGeo Discuss mailing list archive at Nabble.com.

___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-29 Thread P Kishor
On 10/29/08, David Bianco <[EMAIL PROTECTED]> wrote:
> I have experience using Perl and the DBI module to do work like this.  I've
> read from dbf files in the past, but have not written to one.  Still, I see
> no reason why it would be a hangup.Let me know if you'd like to go in
> that direction.

Don't need DBI for this. Just Xbase will do the job in seconds (or
less). Keep in mind, there are two Xbases... one is called XBase and
is part of DBD::XBase, while the other is called just Xbase. I have
used XBase (without any of the DBD hangups), and it is lightning fast
in creating dbf files.




>
> Dave
>
>
>
> On Wed, Oct 29, 2008 at 4:56 PM, Alex Mandel <[EMAIL PROTECTED]>
> wrote:
> >
> > Tyler Erickson wrote:
> >
> > > I am interested in approaches for adding a populated field to a
> shapefile
> > > (for example, adding a new field named 'source_url' with the value
> > > 'http://somewebsite.com').  I would like to do this for several thousand
> > > files.
> > >
> > > At first I thought that I might be able to accomplish it using ogr2org
> with
> > > a sql clause, such as:
> > >
> > > ogr2ogr -sql "select *, 'http://somewebsite.com' as source_url from
> infile"
> > > outfile.shp infile.shp
> > >
> > > but that didn't work since ogr2ogr supports a limited set of SQL,
> described
> > > at:
> > > http://www.gdal.org/ogr/ogr_sql.html
> > >
> > > Any ideas on how to accomplish this? (I would prefer suggestions that
> can be
> > > scripted with python.)
> > >
> > > - Tyler
> > >
> >
> > Open a data connection the dbf with python, add a field to the properties
> of the table and populate the field.
> >
> > There are several ways to connect to a dbf depending on the OS, you could
> use an ODBC connector, or use some other type of dbf driver.
> > I haven't looked closely but there might be a dbf driver in the ogr python
> bindings already.
> >
> > I think my quick fix was to use python windows tools to wrap the dbf com
> object as a python object I could call, but that was a quick a dirty
> solution on windows.
> >
> > The other thing I've done is to create a blank shapefile with the exact
> same scheme + one field. Do an ogr2ogr -f "ESRI Shapefile" -a (append) of
> the blank shapefile and your existing one, and now you have a new field.
> >
> > Alex
> >
> >
> >
> > ___
> > Discuss mailing list
> > Discuss@lists.osgeo.org
> > http://lists.osgeo.org/mailman/listinfo/discuss
> >
>
>
> ___
>  Discuss mailing list
>  Discuss@lists.osgeo.org
>  http://lists.osgeo.org/mailman/listinfo/discuss
>
>


-- 
Puneet Kishor http://punkish.eidesis.org/
Nelson Institute for Environmental Studies http://www.nelson.wisc.edu/
Open Source Geospatial Foundation (OSGeo) http://www.osgeo.org/
___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-29 Thread David Bianco
I have experience using Perl and the DBI module to do work like this.  I've
read from dbf files in the past, but have not written to one.  Still, I see
no reason why it would be a hangup.Let me know if you'd like to go in
that direction.

Dave


On Wed, Oct 29, 2008 at 4:56 PM, Alex Mandel <[EMAIL PROTECTED]>wrote:

> Tyler Erickson wrote:
>
>> I am interested in approaches for adding a populated field to a shapefile
>> (for example, adding a new field named 'source_url' with the value
>> 'http://somewebsite.com').  I would like to do this for several thousand
>> files.
>>
>> At first I thought that I might be able to accomplish it using ogr2org
>> with
>> a sql clause, such as:
>>
>> ogr2ogr -sql "select *, 'http://somewebsite.com' as source_url from
>> infile"
>> outfile.shp infile.shp
>>
>> but that didn't work since ogr2ogr supports a limited set of SQL,
>> described
>> at:
>> http://www.gdal.org/ogr/ogr_sql.html
>>
>> Any ideas on how to accomplish this? (I would prefer suggestions that can
>> be
>> scripted with python.)
>>
>> - Tyler
>>
>
> Open a data connection the dbf with python, add a field to the properties
> of the table and populate the field.
>
> There are several ways to connect to a dbf depending on the OS, you could
> use an ODBC connector, or use some other type of dbf driver.
> I haven't looked closely but there might be a dbf driver in the ogr python
> bindings already.
>
> I think my quick fix was to use python windows tools to wrap the dbf com
> object as a python object I could call, but that was a quick a dirty
> solution on windows.
>
> The other thing I've done is to create a blank shapefile with the exact
> same scheme + one field. Do an ogr2ogr -f "ESRI Shapefile" -a (append) of
> the blank shapefile and your existing one, and now you have a new field.
>
> Alex
>
> ___
> Discuss mailing list
> Discuss@lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/discuss
>
___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


Re: [OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-29 Thread Alex Mandel

Tyler Erickson wrote:

I am interested in approaches for adding a populated field to a shapefile
(for example, adding a new field named 'source_url' with the value
'http://somewebsite.com').  I would like to do this for several thousand
files.

At first I thought that I might be able to accomplish it using ogr2org with
a sql clause, such as:

ogr2ogr -sql "select *, 'http://somewebsite.com' as source_url from infile"
outfile.shp infile.shp

but that didn't work since ogr2ogr supports a limited set of SQL, described
at:
http://www.gdal.org/ogr/ogr_sql.html

Any ideas on how to accomplish this? (I would prefer suggestions that can be
scripted with python.)

- Tyler


Open a data connection the dbf with python, add a field to the 
properties of the table and populate the field.


There are several ways to connect to a dbf depending on the OS, you 
could use an ODBC connector, or use some other type of dbf driver.
I haven't looked closely but there might be a dbf driver in the ogr 
python bindings already.


I think my quick fix was to use python windows tools to wrap the dbf com 
object as a python object I could call, but that was a quick a dirty 
solution on windows.


The other thing I've done is to create a blank shapefile with the exact 
same scheme + one field. Do an ogr2ogr -f "ESRI Shapefile" -a (append) 
of the blank shapefile and your existing one, and now you have a new field.


Alex
___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss


[OSGeo-Discuss] methods for programatically adding fields to shapefiles

2008-10-29 Thread Tyler Erickson

I am interested in approaches for adding a populated field to a shapefile
(for example, adding a new field named 'source_url' with the value
'http://somewebsite.com').  I would like to do this for several thousand
files.

At first I thought that I might be able to accomplish it using ogr2org with
a sql clause, such as:

ogr2ogr -sql "select *, 'http://somewebsite.com' as source_url from infile"
outfile.shp infile.shp

but that didn't work since ogr2ogr supports a limited set of SQL, described
at:
http://www.gdal.org/ogr/ogr_sql.html

Any ideas on how to accomplish this? (I would prefer suggestions that can be
scripted with python.)

- Tyler
-- 
View this message in context: 
http://n2.nabble.com/methods-for-programatically-adding-fields-to-shapefiles-tp1395535p1395535.html
Sent from the OSGeo Discuss mailing list archive at Nabble.com.

___
Discuss mailing list
Discuss@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/discuss