<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hi again,<br>
    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.<br>
    <br>
    So the next step is to suspect the index.intersects result.<br>
    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'.<br>
    What is the internal precision of the QgsPoint?<br>
    My coord attributes are 'double Real 19,10' <br>
    <br>
    In a script, when you select with a point, what is the snapping
    tolerance?<br>
    My project CRS is epsg 4326 - so it should be OK.<br>
    <br>
    FWIW, b.name is string 60 which is copied into a.name string 32  - 
    but the contents are shorter that 32 chars.<br>
    <br>
    Any thoughts anyone?<br>
    <br>
    Regards,<br>
    Zoltan<br>
    <br>
    <br>
    <br>
    <br>
    <div class="moz-cite-prefix">On 2016-07-08 11:00, Zoltan Szecsei
      wrote:<br>
    </div>
    <blockquote
      cite="mid:e174f58e-2f09-e9b8-0b9d-7cbded7baffb@geograph.co.za"
      type="cite">
      <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
      Hi Tim,<br>
      Awesome - my first foray into python & QGIS, after wanting to
      get there more than a year ago. Big thanks for the prod.<br>
      I've changed your code as the layer to be updated was 'a', not
      'b'.<br>
      <br>
      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.<br>
      <br>
      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'.<br>
      I haven't checked them all, but it seems like "all but 1" are
      correct.<br>
      The few I've checked (that are wrong) do have the correct coord
      value to use for selecting a feature from 'b'.<br>
      <br>
      My version is as below - can you give this some thought?<br>
      <br>
      Thanks & keep well,<br>
      Zoltan<br>
      <blockquote>reg = QgsMapLayerRegistry.instance()<br>
        a = reg.mapLayersByName('g21c_2000')[0]<br>
        b = reg.mapLayersByName('CM19_fptn')[0]<br>
        collection = {}<br>
        index = QgsSpatialIndex()<br>
        for f in b.getFeatures():<br>
           index.insertFeature(f)<br>
           collection[f.id()] = f<br>
        with edit(a):<br>
            for af in a.getFeatures():<br>
                p = QgsPoint(af['FptnLong'], af['FptnLat'])<br>
                box = QgsRectangle(p, p) <br>
                for key in index.intersects(box):<br>
                    feature = collection[key]<br>
                    print feature['ID'], af['SGcode'], af['FptnLong'],
        af['FptnLat']<br>
                    af['SGCode'] = feature['ID'] <br>
                    a.updateFeature(af)<br>
      </blockquote>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <br>
      <div class="moz-cite-prefix">On 2016-07-07 23:38, Tim Sutton
        wrote:<br>
      </div>
      <blockquote
        cite="mid:393F571A-531E-49BE-AA9A-9F39F7373A6A@kartoza.com"
        type="cite">
        <meta http-equiv="Content-Type" content="text/html;
          charset=utf-8">
        Hi Zoltan
        <div class=""><br class="">
        </div>
        <div class=""><br class="">
          <div>
            <blockquote type="cite" class="">
              <div class="">On 07 Jul 2016, at 5:55 PM, Zoltan Szecsei
                <<a moz-do-not-send="true"
                  href="mailto:zoltans@geograph.co.za" class="">zoltans@geograph.co.za</a>>
                wrote:</div>
              <br class="Apple-interchange-newline">
              <div class="">
                <meta http-equiv="content-type" content="text/html;
                  charset=utf-8" class="">
                <div bgcolor="#FFFFFF" text="#000000" class=""> Hi
                  Everyone,<br class="">
                  Maybe someone can give me a prod in the right
                  direction?<br class="">
                  <br class="">
                  I have two Shape files (both polygons).<br class="">
                  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.<br class="">
                  <br class="">
                  In other words:<br class="">
                  <blockquote class="">A.shp attribute fields are: 
                    "Name", "Longitude" and "Latitude"<br class="">
                    B.shp attribute field is "Name"<br class="">
                  </blockquote>
                  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.<br class="">
                  <br class="">
                  Is the field calculator up for this?<br class="">
                </div>
              </div>
            </blockquote>
            <div><br class="">
            </div>
            <div>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:</div>
            <div><br class="">
            </div>
            <div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">reg =
                QgsMapLayerRegistry.instance()</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">a =
                reg.mapLayersByName('a')[0]</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">b =
                reg.mapLayersByName('b')[0]</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">collection = {}</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">index = QgsSpatialIndex()</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">for f in b.getFeatures():</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">   index.insertFeature(f)</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">   collection[f.id()] = f</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">with edit(b):</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">    for af in
                a.getFeatures():</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">        p =
                QgsPoint(af['lon'], af['lat'])</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">        box = QgsRectangle(p,
                p) </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">        for key in
                index.intersects(box):</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">            feature =
                collection[key]</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">            print
                feature['name'], af['name'], af['lon'], af['lat']</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">            feature['name'] =
                af['name']</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69);" class="">           
                b.updateFeature(feature)</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class="">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.</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class="">Please test
                in a sandbox before trying on your production data....</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class="">Regards</div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class=""><br
                  class="">
              </div>
              <div style="margin: 0px; line-height: normal; color:
                rgb(69, 69, 69); min-height: 14px;" class="">Tim</div>
            </div>
            <br class="">
            <blockquote type="cite" class="">
              <div class="">
                <div bgcolor="#FFFFFF" text="#000000" class=""> I've
                  been trying to find ways of reference fields in
                  separate maps (layers), but neither Google or I are
                  winning at this.<br class="">
                  <br class="">
                  Regards, and TIA,<br class="">
                  Zoltan<br class="">
                  <br class="">
                  <br class="">
                  <br class="">
                  <br class="">
                  <br class="">
                  <pre class="moz-signature" cols="72">-- 

===========================================
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     <a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="http://www.geograph.co.za/">www.geograph.co.za</a>
===========================================</pre>
                </div>
                _______________________________________________<br
                  class="">
                Africa mailing list<br class="">
                <a moz-do-not-send="true"
                  href="mailto:Africa@lists.osgeo.org" class="">Africa@lists.osgeo.org</a><br
                  class="">
                You can UNSUBSCRIBE at <a moz-do-not-send="true"
                  class="moz-txt-link-freetext"
                  href="http://lists.osgeo.org/mailman/listinfo/africa">http://lists.osgeo.org/mailman/listinfo/africa</a></div>
            </blockquote>
          </div>
          <br class="">
        </div>
        <br>
        <fieldset class="mimeAttachmentHeader"></fieldset>
        <br>
        <pre wrap="">_______________________________________________
Africa mailing list
<a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="mailto:Africa@lists.osgeo.org">Africa@lists.osgeo.org</a>
You can UNSUBSCRIBE at <a moz-do-not-send="true" class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/africa">http://lists.osgeo.org/mailman/listinfo/africa</a></pre>
      </blockquote>
      <br>
      <pre class="moz-signature" cols="72">-- 

===========================================
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     <a moz-do-not-send="true" class="moz-txt-link-abbreviated" href="http://www.geograph.co.za">www.geograph.co.za</a>
===========================================</pre>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
Africa mailing list
<a class="moz-txt-link-abbreviated" href="mailto:Africa@lists.osgeo.org">Africa@lists.osgeo.org</a>
You can UNSUBSCRIBE at <a class="moz-txt-link-freetext" href="http://lists.osgeo.org/mailman/listinfo/africa">http://lists.osgeo.org/mailman/listinfo/africa</a></pre>
    </blockquote>
    <br>
    <pre class="moz-signature" cols="72">-- 

===========================================
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     <a class="moz-txt-link-abbreviated" href="http://www.geograph.co.za">www.geograph.co.za</a>
===========================================</pre>
  </body>
</html>