[OSGeo Africa] QGIS Field Calculator - attributes for 2 layers

Zoltan Szecsei zoltans at geograph.co.za
Fri Jul 8 02:30:56 PDT 2016


Hi again,
OK - it's not an index thing. More than one 'a' polygon has received the 
'b' attribute from the wrong 'b' polygon even though the coordinate is 
inside the correct 'b' polygon.

So the next step is to suspect the index.intersects result.
Note the coords are in LatLong  "small numeric values. I can see we are 
using a null-length box (ie: a point) to select from 'b'.
What is the internal precision of the QgsPoint?
My coord attributes are 'double Real 19,10'

In a script, when you select with a point, what is the snapping tolerance?
My project CRS is epsg 4326 - so it should be OK.

FWIW, b.name is string 60 which is copied into a.name string 32  - but 
the contents are shorter that 32 chars.

Any thoughts anyone?

Regards,
Zoltan




On 2016-07-08 11:00, Zoltan Szecsei wrote:
> Hi Tim,
> Awesome - my first foray into python & QGIS, after wanting to get 
> there more than a year ago. Big thanks for the prod.
> I've changed your code as the layer to be updated was 'a', not 'b'.
>
> So using coordinates from the attribute columns of 'a', I need to 
> update an attribute column of 'a' with an attribute column of the 
> feature in 'b' that was selected by these coords.
>
> The script below works, BUT, it seems like an index is a bit out, 
> because not all the features in 'a' are updated with the correct 
> feature in 'b'.
> I haven't checked them all, but it seems like "all but 1" are correct.
> The few I've checked (that are wrong) do have the correct coord value 
> to use for selecting a feature from 'b'.
>
> My version is as below - can you give this some thought?
>
> Thanks & keep well,
> Zoltan
>
>     reg = QgsMapLayerRegistry.instance()
>     a = reg.mapLayersByName('g21c_2000')[0]
>     b = reg.mapLayersByName('CM19_fptn')[0]
>     collection = {}
>     index = QgsSpatialIndex()
>     for f in b.getFeatures():
>        index.insertFeature(f)
>        collection[f.id()] = f
>     with edit(a):
>         for af in a.getFeatures():
>             p = QgsPoint(af['FptnLong'], af['FptnLat'])
>             box = QgsRectangle(p, p)
>             for key in index.intersects(box):
>                 feature = collection[key]
>                 print feature['ID'], af['SGcode'], af['FptnLong'],
>     af['FptnLat']
>                 af['SGCode'] = feature['ID']
>                 a.updateFeature(af)
>
>
>
>
>
>
>
>
> On 2016-07-07 23:38, Tim Sutton wrote:
>> Hi Zoltan
>>
>>
>>> On 07 Jul 2016, at 5:55 PM, Zoltan Szecsei <zoltans at geograph.co.za 
>>> <mailto:zoltans at geograph.co.za>> wrote:
>>>
>>> Hi Everyone,
>>> Maybe someone can give me a prod in the right direction?
>>>
>>> I have two Shape files (both polygons).
>>> I'm trying to copy an attribute field value from B.shp into A.shp, 
>>> using Lon and Lat from two of A.shp attribute fields to select the 
>>> relevant polygon's attribute in B.shp.
>>>
>>> In other words:
>>>
>>>     A.shp attribute fields are: "Name", "Longitude" and "Latitude"
>>>     B.shp attribute field is "Name"
>>>
>>> Using the LatLong coords in the attribute fields of A.shp, I want to 
>>> select a polygon in B.shp, and write that polygons "Name" contents 
>>> into this A.shp's "Name" field.
>>>
>>> Is the field calculator up for this?
>>
>> I don't think so - its not really set up for doing inter-layer 
>> references. I wrote a few lines of python that will do it though 
>> (assumes you have two layers loaded with names a and b in the layers 
>> list:
>>
>> reg = QgsMapLayerRegistry.instance()
>> a = reg.mapLayersByName('a')[0]
>> b = reg.mapLayersByName('b')[0]
>> collection = {}
>> index = QgsSpatialIndex()
>> for f in b.getFeatures():
>>    index.insertFeature(f)
>>    collection[f.id()] = f
>>
>> with edit(b):
>>     for af in a.getFeatures():
>>         p = QgsPoint(af['lon'], af['lat'])
>>         box = QgsRectangle(p, p)
>>         for key in index.intersects(box):
>>             feature = collection[key]
>>             print feature['name'], af['name'], af['lon'], af['lat']
>>             feature['name'] = af['name']
>> b.updateFeature(feature)
>>
>>
>>
>> Just paste that into the QGIS python console and it will update the 
>> name field of b with the value in a for the places where the 
>> attributes lon, lat in a intersect b.
>>
>> Please test in a sandbox before trying on your production data....
>>
>> Regards
>>
>> Tim
>>
>>> I've been trying to find ways of reference fields in separate maps 
>>> (layers), but neither Google or I are winning at this.
>>>
>>> Regards, and TIA,
>>> Zoltan
>>>
>>>
>>>
>>>
>>>
>>> -- 
>>>
>>> ===========================================
>>> Zoltan Szecsei PrGISc [PGP0031]
>>> Geograph (Pty) Ltd.
>>> GIS and Photogrammetric Services
>>>
>>> P.O. Box 7, Muizenberg 7950, South Africa.
>>>
>>> Mobile: +27-83-6004028
>>> Fax:    +27-86-6115323www.geograph.co.za
>>> ===========================================
>>> _______________________________________________
>>> Africa mailing list
>>> Africa at lists.osgeo.org <mailto:Africa at lists.osgeo.org>
>>> You can UNSUBSCRIBE at http://lists.osgeo.org/mailman/listinfo/africa
>>
>>
>>
>> _______________________________________________
>> Africa mailing list
>> Africa at lists.osgeo.org
>> You can UNSUBSCRIBE athttp://lists.osgeo.org/mailman/listinfo/africa
>
> -- 
>
> ===========================================
> Zoltan Szecsei PrGISc [PGP0031]
> Geograph (Pty) Ltd.
> GIS and Photogrammetric Services
>
> P.O. Box 7, Muizenberg 7950, South Africa.
>
> Mobile: +27-83-6004028
> Fax:    +27-86-6115323www.geograph.co.za
> ===========================================
>
>
> _______________________________________________
> Africa mailing list
> Africa at lists.osgeo.org
> You can UNSUBSCRIBE at http://lists.osgeo.org/mailman/listinfo/africa

-- 

===========================================
Zoltan Szecsei PrGISc [PGP0031]
Geograph (Pty) Ltd.
GIS and Photogrammetric Services

P.O. Box 7, Muizenberg 7950, South Africa.

Mobile: +27-83-6004028
Fax:    +27-86-6115323     www.geograph.co.za
===========================================

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/africa/attachments/20160708/b32383fa/attachment-0001.html>


More information about the Africa mailing list