[FWTools] OGR2OGR QUOTES ISSUE

Matt Wilkie matt.wilkie at gov.yk.ca
Thu Oct 7 16:50:29 EST 2010


  ahh, now we're closing in on it.

    "\"LAYER='  "+line+"\'  "

should be (spaces added for easier reading):

    ' "LAYER=' + line + ' " '

however there is a way to write this which is not so hard to pick apart 
in your head: using the % substition operator. In the following /%s /is 
changed to value of /line /when interpreted.

 >>> line = 'T21-PUENTES'
 >>> query = '-where "LAYER=\'%s\' " ' % (line)
 >>> print query
-where "LAYER='T21-PUENTES'"

Sometimes it helps to use the string replace method to temporarily use a 
character other than a quote when building the expression (produces 
identical results).

 >>> query = "-where #LAYER='%s'#" % (line)
 >>> query = query.replace('#','"')
 >>> print query
-where "LAYER='T21-PUENTES'"

So to wrap it up altogether:

ogr2ogr = 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe'
line = 'T21-PUENTES'
b = 'tapalpa_05_plani_line'
inshape = line + '.shp'
outshape = b + '.shp'
query = "-where #LAYER='%s'#" % (line)
query = query.replace('#','"')
print ogr2ogr, inshape, query,  outshape
# p = subprocess.Popen([ogr2ogr, inshape, query,  outshape])



matt wilkie
--------------------------------------------
Geomatics Analyst
Information Management and Technology
Yukon Department of Environment
10 Burns Road * Whitehorse, Yukon * Y1A 4Y9
867-667-8133 Tel * 867-393-7003 Fax
http://environmentyukon.gov.yk.ca/geomatics/
--------------------------------------------


On 07/10/2010 11:05 AM, Susana Iraiis Delgado Rodriguez wrote:
> Hello Matt!
> Thank you for your advice, but see the problem is that I cannot read 
> the value from the variable line, it shuold be something like +line+.
> The code you suggested is telling the subprocess to read line as 
> string "line" instead of execuitng the query, same will happen with 
> the other script. I almost got to the answer:
> print 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', 
> '-where', "\"LAYER='  "+line+"\'  ", b+'.shp'
> I'm doing a print to see the ouput before it executes the command, now 
> mi output sentence is: C:/Archivos de 
> programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES
> .shp -where "LAYER='T21-PUENTES' tapalpa_05_plani_line.shp
> I`m missing a (") after it gets the value of line. I don't know how to 
> close the statement
> Regards
>
> 2010/10/7 Matt Wilkie <matt.wilkie at gov.yk.ca 
> <mailto:matt.wilkie at gov.yk.ca>>
>
>     Hi Susana,
>
>     You're missing the beginning single quote (') before
>     C:/Archivos.... and I don't think you need to escape the inner
>     double quote ("), so instead of
>
>         '\"LAYER= \'line\'\"'
>
>     try
>
>         '"LAYER=\'line\'"'
>
>     Also it would be easier to read the code if you did something like:
>
>     ogr2ogr = 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr'
>     inshape = line + '.shp'
>     outshape = b + '.shp'
>     query = '-where "LAYER=\'line\'" '
>
>     p = subprocess.Popen([ogr2ogr, inshape, query,  outshape])
>
>     hope that helps,
>
>     matt wilkie
>     --------------------------------------------
>     Geomatics Analyst
>     Information Management and Technology
>     Yukon Department of Environment
>     10 Burns Road * Whitehorse, Yukon * Y1A 4Y9
>     867-667-8133 Tel * 867-393-7003 Fax
>     http://environmentyukon.gov.yk.ca/geomatics/
>     --------------------------------------------
>
>
>     On 07/10/2010 9:19 AM, Susana Iraiis Delgado Rodriguez wrote:
>>     Hello members!
>>     I'm running a python script to execute an ogr2ogr command. The
>>     process has been sucessful, but now I want to create a shp for
>>     each layer a shp has. I now I must use the command :
>>     ogr2ogr newX.shp -where "LAYER = 'layerValueX'" original.shp
>>     The code for the subprocess is as follows:
>>     for line in open("unico.txt", "r").readlines():
>>       p = subprocess.Popen([C:/Archivos de
>>     programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp',
>>     ' -where', '\"LAYER= \'line\'\"', b+'.shp'])unfortuanely wtih
>>     this code the module reads the variable line, which as the value
>>     of the layer has a string "line": The problem is located after
>>     the word LAYER, I don't have idea about the properly way to write
>>     the quotes.
>>     Hope you can help, regards!
>>
>>
>
>     _______________________________________________
>     FWTools mailing list
>     FWTools at lists.maptools.org <mailto:FWTools at lists.maptools.org>
>     http://lists.maptools.org/mailman/listinfo/fwtools
>     http://fwtools.maptools.org/
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/fwtools/attachments/20101007/17208fa6/attachment.htm 


More information about the FWTools mailing list