[Gvsig_english] How to access schema from an input shapefile in scripting
Óscar Martínez
omartinez at gvsig.com
Fri Oct 14 11:26:55 CEST 2016
Hi Silvia,
| Is there a way to know which functions are implemented for each
feature type in gvSIG scripting?
If i understand correctly your question.. Quick answer, all. Since 2.3,
we are not working with a different library for scripting. We improved
the way to work directly with the JAVA API, plus new functions (like..
createShape(schema)), methods (like.. getValues() over a feature to
return a python dict) and new classes (like.. FormPanel to create
scripts with graphical interface), all to make it easy and quick to work
from scripting. Not sure if is a good way to say it (just to explain
this better), but we could say right now is the java api who hasn't have
all the funcions implemented.
As you can see in the script, type(layer) and type(features) are java
objects.
Running script.
layer type: <type 'org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect'>
layer features: <type
'org.gvsig.fmap.dal.feature.impl.featureset.DefaultFeatureSet'>
Script terminated.
So, you should be able to check the javadocs, and use all the functions
that are in there:
http://downloads.gvsig.org/download/gvsig-desktop-testing/dists/2.3.0/javadocs/html/
http://downloads.gvsig.org/download/gvsig-desktop-testing/dists/2.3.0/javadocs/html/org/gvsig/fmap/mapcontext/layers/vectorial/FLyrVect.html
http://downloads.gvsig.org/download/gvsig-desktop-testing/dists/2.3.0/javadocs/html/org/gvsig/fmap/dal/feature/impl/featureset/DefaultFeatureSet.html
Plus all from scripting (we are updating this javadoc, soon will be more
complete):
http://downloads.gvsig.org/download/gvsig-desktop-testing/dists/2.3.0/docs/javadocs/html/
Deleting features should be something similar to this (delete all
features with a filter "ID < 10"):
# encoding: utf-8
from gvsig import *
def main(*args):
""" Delete features inside a layer"""
layer = currentLayer()
features = layer.features("ID < 10") #DefaultFeatureSet
print "layer type: ", type(layer)
print "layer features: ", type(features)
layer.edit()
for i in features:
ivalues = i.getValues()
print ivalues
features.delete(i)#delete over the featureset
layer.commit()
Best regards,
Óscar
El 14/10/16 a las 10:53, Silvia Franceschi escribió:
> Hi Óscar,
> the scripts work fine, many thanks again!
> May I just ask you one more question?
> Is there the possibility to remove a feature from a list of features?
> like featureslist.remove() or something like that?
> I tried with remove and delete but it seems that these do not exist,
> any idea?
>
> Is there a way to know which functions are implemented for each
> feature type in gvSIG scripting?
>
> Thanks
>
> Silvia
>
>
> On Thu, Oct 13, 2016 at 6:25 PM, Silvia Franceschi
> <silvia.franceschi at gmail.com <mailto:silvia.franceschi at gmail.com>> wrote:
>
> Thank you Óscar,
> to tell you the truth I started to use currentLayer but, since I
> need to work with two layers I didn't know how to do and tried to
> load data from files.
> And in any case the final script should work with a list of
> shapefiles in a folder, so, I will for sure move to the file based
> data source at some point.
>
> I was following the documentation you linked, it is very useful to
> start to get familiar with the scripting environment, thanks!
>
> I will try the scripts you sent to me and let you know if I will
> solve my problems.
>
> Thank you very much for the quick answer!
>
> Silvia
>
>
>
>
> On Thu, Oct 13, 2016 at 6:00 PM, Óscar Martínez
> <omartinez at gvsig.com <mailto:omartinez at gvsig.com>> wrote:
>
> Hi!
>
> If i understood correctly, you don't need to use the getSchema
> function. I'm going to give you my solution with two test
> layers, give it a try and tell us if that works for you or
> what exactly more do you need. If you don't understand some
> part of the script i can explain it better. Some help is
> inside the script, step by step.
>
> I recommend to you to start learning scripting using the
> functions currentLayer() (current layer selection in the table
> of contents) or currentView().getLayer("layername") instead of
> using loadShapeFile everytime. It's easier if you just have a
> loaded layer in your view and access to each one each layer
> with currentLayer o getLayer.
>
> If someone on the list want to know more about this part of
> scripting, here are some links from the spanish docs (hope
> soon in english) related to this post:
>
> Access to features:
> http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/acceso_a_objetos.html#entidad
> <http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/acceso_a_objetos.html#entidad>
>
> Spatial operations:
> http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/modulo_geom.html#operaciones-espaciales
> <http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/modulo_geom.html#operaciones-espaciales>
>
> Current functions:
> http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/acceso_a_objetos.html
> <http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/acceso_a_objetos.html>
>
>
> Any trouble here we are!
>
>
> Best regards,
>
> Óscar
>
>
>
> Script (layers of this test are in EPSG:25830) (feature AREA1
> is a multipolygon):
>
> # encoding: utf-8
>
> import gvsig
>
> def main(*args):
> # You need to have one view with to layers opened on it
> # The name of the layers will be the name that appear
> # in the table of contents of the view
>
> # To access to the layers that are already loaded in gvSIG
> # gvsig.currentView() give us access to the opened view
> # gvsig.currentView().getLayer(name) give us access a
> specified layer
> points = gvsig.currentView().getLayer("points")
> area = gvsig.currentView().getLayer("area")
>
> # Access to the features
> features_points = points.features()
> features_area = area.features()
>
> # if you want access to a selection of features, not all
> # you will have to change `features()` with `getSelection()`
> #features_points = points.getSelection()
> #features_area = area.getSelection()
>
> for farea in features_area:
> print "\nChecking : ", farea.get("localId")
> geom_farea = farea.geometry()
>
> #for each area, check all points without intersection
> for fpoint in features_points:
> geom_fpoint = fpoint.geometry()
>
> if not geom_farea.intersects(geom_fpoint):
> print "\tArea: ", farea.get("localId"), "
> intersects with :", fpoint.get("ID")
> # work with the feature
>
> Console output:
>
> Running script testing_silvia_intersec.
>
> Checking : AREA1
> Area: AREA1 intersects with : 101
> Area: AREA1 intersects with : 102
> Area: AREA1 intersects with : 103
> Area: AREA1 intersects with : 107
>
> Checking : AREA2
> Area: AREA2 intersects with : 103
> Area: AREA2 intersects with : 104
> Area: AREA2 intersects with : 105
> Area: AREA2 intersects with : 106
> Area: AREA2 intersects with : 107
> Area: AREA2 intersects with : 108
> Script testing_silvia_intersec terminated.
>
>
>
> El 13/10/16 a las 15:05, Silvia Franceschi escribió:
>> Hi all,
>> I am trying to write my first script in gvSIG and Python, I
>> need to read a point shapefile and filter some elements that
>> are outside a defined area.
>> To do this I have to read two input layers, one with the
>> points and one with the area and then work on each single
>> features of the point layer.
>> I started with the selection of the input layers using a
>> filechooser dialog (commonsdialog.filechooser) and then I
>> need to obtain the schema of these layers, but it seems that
>> the object filechooser do not have the .getSchema() function.
>> Do you have any idea on how to obtain the schema from a
>> vector layer selected using the filechooser?
>>
>> Thanks in advance for any help!
>>
>> Silvia
>>
>>
>> --
>> ing. Silvia Franceschi
>> Via Latemar, 22
>> 38030 Castello di Fiemme (TN)
>>
>> tel: 0039 -3384501332 <tel:0039%20-3384501332>
>>
>>
>> _______________________________________________
>> Gvsig_internacional mailing list
>> Gvsig_internacional at listserv.gva.es
>> <mailto:Gvsig_internacional at listserv.gva.es>
>>
>> To see the archives, edit your preferences or unsubscribe from this mailing list, please access this url:
>>
>> https://listserv.gva.es/cgi-bin/mailman/listinfo/gvsig_internacional
>> <https://listserv.gva.es/cgi-bin/mailman/listinfo/gvsig_internacional>
> _______________________________________________
> Gvsig_internacional mailing list
> Gvsig_internacional at listserv.gva.es
> <mailto:Gvsig_internacional at listserv.gva.es> To see the
> archives, edit your preferences or unsubscribe from this
> mailing list, please access this url:
> https://listserv.gva.es/cgi-bin/mailman/listinfo/gvsig_internacional
> <https://listserv.gva.es/cgi-bin/mailman/listinfo/gvsig_internacional>
>
>
> --
> ing. Silvia Franceschi Via Latemar, 22
> 38030 Castello di Fiemme (TN) tel: 0039 -3384501332
> <tel:0039%20-3384501332>
>
> --
> ing. Silvia Franceschi Via Latemar, 22
> 38030 Castello di Fiemme (TN) tel: 0039 -3384501332
>
> _______________________________________________
> Gvsig_internacional mailing list
> Gvsig_internacional at listserv.gva.es
>
> To see the archives, edit your preferences or unsubscribe from this mailing list, please access this url:
>
> https://listserv.gva.es/cgi-bin/mailman/listinfo/gvsig_internacional
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listserv.gva.es/pipermail/gvsig_internacional/attachments/20161014/ea741b58/attachment.html>
More information about the Gvsig_internacional
mailing list