Clipping shapefiles with ogr2ogr

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Clipping shapefiles with ogr2ogr

Alyre Chiasson
Hello,

I have two shape files with the following identical prj :

GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]

I want to cut a rectangle with the following coordinates

xmin –68.9125
ymin 44.129
xmax –63.0701
ymax 48.3721

from a shapefile with the following coordinates:

xmin –69.35
ymin 43.45
xmax –50.85
ymax 60.95

The way I read it the smaller rectangle should lay totally within the
larger. So I have tried:

ogr2ogr –spat –68.9125 44.129 –63.0701 48.3721 out.shp in.shp

The does produce a new file, however, the metadata have not changed,
QGIS says it is the same as the original. In addition when I overlay the
out.shp over the in.shp, I do not have a smaller rectangle within a
larger. What I have is almost a diagonal swath retained from the
original file with two cookie cutter holes of contour lines missing in
the lower portion. The original shapefile contains contour lines for
temperature. The cookie cutter holes are areas with closed polygons that
are close to being spherical.

What am I missing here? Does the spat option do something other than
clipping? Seemed straight forward but the results are strange.

Thanks

Alyre
_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users
Reply | Threaded
Open this post in threaded view
|

Re: Clipping shapefiles with ogr2ogr

Paul Spencer-2
My first guess is that ogr2ogr doesn't clip, it uses the bbox as a  
spatial selection and keeps anything that intersects with the bbox.  
Would this account for your results?

Paul

On 17-Feb-08, at 9:25 PM, Alyre Chiasson wrote:

> Hello,
>
> I have two shape files with the following identical prj :
>
> GEOGCS
> ["GCS_North_American_1983
> ",DATUM["D_North_American_1983",SPHEROID["GRS_1980",
> 6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",
> 0.0174532925199433]]
>
> I want to cut a rectangle with the following coordinates
>
> xmin –68.9125
> ymin 44.129
> xmax –63.0701
> ymax 48.3721
>
> from a shapefile with the following coordinates:
>
> xmin –69.35
> ymin 43.45
> xmax –50.85
> ymax 60.95
>
> The way I read it the smaller rectangle should lay totally within  
> the larger. So I have tried:
>
> ogr2ogr –spat –68.9125 44.129 –63.0701 48.3721 out.shp in.shp
>
> The does produce a new file, however, the metadata have not changed,  
> QGIS says it is the same as the original. In addition when I overlay  
> the out.shp over the in.shp, I do not have a smaller rectangle  
> within a larger. What I have is almost a diagonal swath retained  
> from the original file with two cookie cutter holes of contour lines  
> missing in the lower portion. The original shapefile contains  
> contour lines for temperature. The cookie cutter holes are areas  
> with closed polygons that are close to being spherical.
>
> What am I missing here? Does the spat option do something other than  
> clipping? Seemed straight forward but the results are strange.
>
> Thanks
>
> Alyre
> _______________________________________________
> Ottawa_users mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/ottawa_users


__________________________________________

    Paul Spencer
    Chief Technology Officer
    DM Solutions Group Inc
    http://www.dmsolutions.ca/

_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users
Reply | Threaded
Open this post in threaded view
|

Re: Clipping shapefiles with ogr2ogr

Tyler Mitchell (OSGeo)
On 18-Feb-08, at 6:22 AM, Paul Spencer wrote:

> My first guess is that ogr2ogr doesn't clip, it uses the bbox as a  
> spatial selection and keeps anything that intersects with the  
> bbox.  Would this account for your results?

That's right, it cannot clip, but will select/return all features  
that fit in the area.  You can't use ogr2ogr for clipping but I  
believe you could write a Python script that calls the OGR and GEOS  
functions to do this job.

Tyler
_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users
Reply | Threaded
Open this post in threaded view
|

Re: Clipping shapefiles with ogr2ogr

Alyre Chiasson
In reply to this post by Alyre Chiasson
Hello,

I suspected that something like below was happening, although the documentation I read regarding the -spat option presents it as a clipping function. I was surprised by the result. I am new to all of this and have no computer science background so I am not familar with a "bbox" . Although I know Python is a flexible multi platform language (I see a lot of references in the Linux world), attempting to learn the language to write a script to do this job or to understand someone else's script I believe would be beyond my time constraints for the current project. I did try to import both shape files into GRASS as layers and then tried to overlay (v.overlay) them but got error messages which I couldn't understand but were basically complaining about missing attributes. Not sure what attributes it was referring to. Is there actually no way to do this within GRASS or QGIS that retains the feature of the shapefile (everthing within the clipping region) without having to learn a programming language? Would seem to be a desirable function. Not adverse to learning a program language but it would be a much longer term goal.

Thanks

Alyre



----- Original Message ----
From: Tyler Mitchell (OSGeo) <[hidden email]>
To: Ottawa (Canada) Local Chapter List <[hidden email]>
Sent: Monday, February 18, 2008 12:26:36 PM
Subject: Re: [Ottawa_users] Clipping shapefiles with ogr2ogr

On 18-Feb-08, at 6:22 AM, Paul Spencer wrote:

> My first guess is that ogr2ogr doesn't clip, it uses the bbox as a 
> spatial selection and keeps anything that intersects with the 
> bbox.  Would this account for your results?

That's right, it cannot clip, but will select/return all features 
that fit in the area.  You can't use ogr2ogr for clipping but I 
believe you could write a Python script that calls the OGR and GEOS 
functions to do this job.

Tyler
_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users



Ask a question on any topic and get answers from real people. Go to Yahoo! Answers.
_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users
Reply | Threaded
Open this post in threaded view
|

Re: Clipping shapefiles with ogr2ogr

Christy Nieman-2
Hi,

I just spent a while working on clipping in QGIS/GRASS, and it is quite
simple.  I used the GRASS Tools plugin for QGIS to load my shapefiles
into a GRASS mapset.  I then converted my mapset's region to a GRASS
vector layer to use as the overlay layer in the clip (v.in.region -
under Vector/Create standard vectors in the QGIS GRASS tools dialog).  
Then I used the v.overlay intersection tool to do the clip.  This has to
be done one layer at a time.

If you need more details I can provide a specific example.

Christy

Alyre Chiasson wrote:

> Hello,
>
> I suspected that something like below was happening, although the
> documentation I read regarding the -spat option presents it as a
> clipping function. I was surprised by the result. I am new to all of
> this and have no computer science background so I am not familar with
> a "bbox" . Although I know Python is a flexible multi platform
> language (I see a lot of references in the Linux world), attempting to
> learn the language to write a script to do this job or to understand
> someone else's script I believe would be beyond my time constraints
> for the current project. I did try to import both shape files into
> GRASS as layers and then tried to overlay (v.overlay) them but got
> error messages which I couldn't understand but were basically
> complaining about missing attributes. Not sure what attributes it was
> referring to. Is there actually no way to do this within GRASS or QGIS
> that retains the feature of the shapefile (everthing within the
> clipping region) without having to learn a programming language? Would
> seem to be a desirable function. Not adverse to learning a program
> language but it would be a much longer term goal.
>
> Thanks
>
> Alyre
>
>
>
> ----- Original Message ----
> From: Tyler Mitchell (OSGeo) <[hidden email]>
> To: Ottawa (Canada) Local Chapter List <[hidden email]>
> Sent: Monday, February 18, 2008 12:26:36 PM
> Subject: Re: [Ottawa_users] Clipping shapefiles with ogr2ogr
>
> On 18-Feb-08, at 6:22 AM, Paul Spencer wrote:
>
> > My first guess is that ogr2ogr doesn't clip, it uses the bbox as a
> > spatial selection and keeps anything that intersects with the
> > bbox.  Would this account for your results?
>
> That's right, it cannot clip, but will select/return all features
> that fit in the area.  You can't use ogr2ogr for clipping but I
> believe you could write a Python script that calls the OGR and GEOS
> functions to do this job.
>
> Tyler
> _______________________________________________
> Ottawa_users mailing list
> [hidden email] <mailto:[hidden email]>
> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>
>
> ------------------------------------------------------------------------
> Ask a question on any topic and get answers from real people. *Go to
> Yahoo! Answers.* <http://ca.answers.yahoo.com>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Ottawa_users mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>  
_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users
Reply | Threaded
Open this post in threaded view
|

Re: Clipping shapefiles with ogr2ogr

Alyre Chiasson-2
Hi Christy,

Thanks for the offer of help. I believe I understand your instructions.
I have gotten as far as having the GRASS clip mask and the underlying
GRASS layer from which I want to retain the underlying feature. So if I
could cut out what is under the mask, I would have my desired result.
Look good at this stage on the screen. However, when I execute the
v.overlay I get the following error, "Database connection not defined
for layer 1" No layer is created and QGIS freezes then crashes when I
try to exit. Any idea what this message means. Seems like some related
to the database pathway? Did something not get properly imported from
the shapefiles? How do I correct it?


Thanks

Alyre




Loader

Christy Nieman wrote:

> Hi,
>
> I just spent a while working on clipping in QGIS/GRASS, and it is
> quite simple.  I used the GRASS Tools plugin for QGIS to load my
> shapefiles into a GRASS mapset.  I then converted my mapset's region
> to a GRASS vector layer to use as the overlay layer in the clip
> (v.in.region - under Vector/Create standard vectors in the QGIS GRASS
> tools dialog).  Then I used the v.overlay intersection tool to do the
> clip.  This has to be done one layer at a time.
>
> If you need more details I can provide a specific example.
>
> Christy
>
> Alyre Chiasson wrote:
>> Hello,
>>
>> I suspected that something like below was happening, although the
>> documentation I read regarding the -spat option presents it as a
>> clipping function. I was surprised by the result. I am new to all of
>> this and have no computer science background so I am not familar with
>> a "bbox" . Although I know Python is a flexible multi platform
>> language (I see a lot of references in the Linux world), attempting
>> to learn the language to write a script to do this job or to
>> understand someone else's script I believe would be beyond my time
>> constraints for the current project. I did try to import both shape
>> files into GRASS as layers and then tried to overlay (v.overlay) them
>> but got error messages which I couldn't understand but were basically
>> complaining about missing attributes. Not sure what attributes it was
>> referring to. Is there actually no way to do this within GRASS or
>> QGIS that retains the feature of the shapefile (everthing within the
>> clipping region) without having to learn a programming language?
>> Would seem to be a desirable function. Not adverse to learning a
>> program language but it would be a much longer term goal.
>>
>> Thanks
>>
>> Alyre
>>
>>
>>
>> ----- Original Message ----
>> From: Tyler Mitchell (OSGeo) <[hidden email]>
>> To: Ottawa (Canada) Local Chapter List <[hidden email]>
>> Sent: Monday, February 18, 2008 12:26:36 PM
>> Subject: Re: [Ottawa_users] Clipping shapefiles with ogr2ogr
>>
>> On 18-Feb-08, at 6:22 AM, Paul Spencer wrote:
>>
>> > My first guess is that ogr2ogr doesn't clip, it uses the bbox as a
>> > spatial selection and keeps anything that intersects with the >
>> bbox.  Would this account for your results?
>>
>> That's right, it cannot clip, but will select/return all features
>> that fit in the area.  You can't use ogr2ogr for clipping but I
>> believe you could write a Python script that calls the OGR and GEOS
>> functions to do this job.
>>
>> Tyler
>> _______________________________________________
>> Ottawa_users mailing list
>> [hidden email] <mailto:[hidden email]>
>> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>>
>>
>> ------------------------------------------------------------------------
>> Ask a question on any topic and get answers from real people. *Go to
>> Yahoo! Answers.* <http://ca.answers.yahoo.com>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> Ottawa_users mailing list
>> [hidden email]
>> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>>  
> _______________________________________________
> Ottawa_users mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>

_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users
Reply | Threaded
Open this post in threaded view
|

Re: Clipping shapefiles with ogr2ogr

Christy Nieman-2
I have no idea.  My only suggestion would be to try loading the
shapefile into the GRASS mapset again.

Christy

Alyre Chiasson wrote:

> Hi Christy,
>
> Thanks for the offer of help. I believe I understand your
> instructions. I have gotten as far as having the GRASS clip mask and
> the underlying GRASS layer from which I want to retain the underlying
> feature. So if I could cut out what is under the mask, I would have my
> desired result. Look good at this stage on the screen. However, when I
> execute the v.overlay I get the following error, "Database connection
> not defined for layer 1" No layer is created and QGIS freezes then
> crashes when I try to exit. Any idea what this message means. Seems
> like some related to the database pathway? Did something not get
> properly imported from the shapefiles? How do I correct it?
>
>
> Thanks
>
> Alyre
>
>
>
>
> Loader
>
> Christy Nieman wrote:
>> Hi,
>>
>> I just spent a while working on clipping in QGIS/GRASS, and it is
>> quite simple.  I used the GRASS Tools plugin for QGIS to load my
>> shapefiles into a GRASS mapset.  I then converted my mapset's region
>> to a GRASS vector layer to use as the overlay layer in the clip
>> (v.in.region - under Vector/Create standard vectors in the QGIS GRASS
>> tools dialog).  Then I used the v.overlay intersection tool to do the
>> clip.  This has to be done one layer at a time.
>>
>> If you need more details I can provide a specific example.
>>
>> Christy
>>
>> Alyre Chiasson wrote:
>>> Hello,
>>>
>>> I suspected that something like below was happening, although the
>>> documentation I read regarding the -spat option presents it as a
>>> clipping function. I was surprised by the result. I am new to all of
>>> this and have no computer science background so I am not familar
>>> with a "bbox" . Although I know Python is a flexible multi platform
>>> language (I see a lot of references in the Linux world), attempting
>>> to learn the language to write a script to do this job or to
>>> understand someone else's script I believe would be beyond my time
>>> constraints for the current project. I did try to import both shape
>>> files into GRASS as layers and then tried to overlay (v.overlay)
>>> them but got error messages which I couldn't understand but were
>>> basically complaining about missing attributes. Not sure what
>>> attributes it was referring to. Is there actually no way to do this
>>> within GRASS or QGIS that retains the feature of the shapefile
>>> (everthing within the clipping region) without having to learn a
>>> programming language? Would seem to be a desirable function. Not
>>> adverse to learning a program language but it would be a much longer
>>> term goal.
>>>
>>> Thanks
>>>
>>> Alyre
>>>
>>>
>>>
>>> ----- Original Message ----
>>> From: Tyler Mitchell (OSGeo) <[hidden email]>
>>> To: Ottawa (Canada) Local Chapter List <[hidden email]>
>>> Sent: Monday, February 18, 2008 12:26:36 PM
>>> Subject: Re: [Ottawa_users] Clipping shapefiles with ogr2ogr
>>>
>>> On 18-Feb-08, at 6:22 AM, Paul Spencer wrote:
>>>
>>> > My first guess is that ogr2ogr doesn't clip, it uses the bbox as a
>>> > spatial selection and keeps anything that intersects with the >
>>> bbox.  Would this account for your results?
>>>
>>> That's right, it cannot clip, but will select/return all features
>>> that fit in the area.  You can't use ogr2ogr for clipping but I
>>> believe you could write a Python script that calls the OGR and GEOS
>>> functions to do this job.
>>>
>>> Tyler
>>> _______________________________________________
>>> Ottawa_users mailing list
>>> [hidden email] <mailto:[hidden email]>
>>> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>>>
>>>
>>> ------------------------------------------------------------------------
>>>
>>> Ask a question on any topic and get answers from real people. *Go to
>>> Yahoo! Answers.* <http://ca.answers.yahoo.com>
>>> ------------------------------------------------------------------------
>>>
>>>
>>> _______________________________________________
>>> Ottawa_users mailing list
>>> [hidden email]
>>> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>>>  
>> _______________________________________________
>> Ottawa_users mailing list
>> [hidden email]
>> http://lists.osgeo.org/mailman/listinfo/ottawa_users
>>
>
> _______________________________________________
> Ottawa_users mailing list
> [hidden email]
> http://lists.osgeo.org/mailman/listinfo/ottawa_users
_______________________________________________
Ottawa_users mailing list
[hidden email]
http://lists.osgeo.org/mailman/listinfo/ottawa_users