count points where is within the polygons using shapely and fiona

Peter Pearson pkpearson at nowhere.invalid
Sun Jan 29 20:06:24 EST 2017


On Sun, 29 Jan 2017 11:42:47 -0800 (PST), Xristos Xristoou wrote:
> i tried to count points from the point shapefile where is within in
> the polygon shapefile but i fail.Maybe my code is complete wrong but i
> tried.
> any idea how to fix my code ?
> i want fast method because i have big data features
> from shapely.geometry import shape
> import fiona
>
> filepath1 = "point.shp"
> file1features = []
> intersectfeatures_file1 = []
> intersectfeatures_file2 = []
> count =0
> count=count+1

This is an illogical place to increment count.

> outschema = None
> with fiona.collection(filepath1, "r") as input1:
>     outschema = input1.schema.copy()
>     for p1 in input1:
>         file1features.append(p1)

I believe you could also say file1features = list(input1).


> filepath2 = "polygon.shp"
> with fiona.collection(filepath2, "r") as input2:
>     for p2 in input2:
>         for p1 in file1features:
>             [count for i in [shape(p1['geometry']).within(shape(p2['geometry']))]]

The above line produces a list that is discarded because it is not
assigned any name.

>             if p1 not in intersectfeatures_file1:
>                 intersectfeatures_file1.append(p1)
>             if p2 not in intersectfeatures_file2:
>                 intersectfeatures_file2.append(p2)
>
> print count

> '''
> if intersectfeatures_file1:
>     outfile = "outputfile1.shp"
>     with fiona.collection(outfile, "w", "ESRI Shapefile", outschema) as output:
>         for outfeature in intersectfeatures:
>             output.write(outfeature)
> if intersectfeatures_file2:
>     outfile = "outputfile2.shp"
>     with fiona.collection(outfile, "w", "ESRI Shapefile", outschema) as output:
>         for outfeature in intersectfeatures:
>             output.write(outfeature)
> '''

The above lines between triple quotes should have been omitted
from this post.

> on the print count i take 20 times the number 1. after for this count i want to export to new shapefile the polygons where have specific number of points. thnx


-- 
To email me, substitute nowhere->runbox, invalid->com.


More information about the Python-list mailing list