[Gvsig_english] How to access schema from an input shapefile in scripting
Óscar Martínez
omartinez at gvsig.com
Wed Oct 19 11:00:56 CEST 2016
In the 3D Points script it appears this full solution, creating a new
shape to export the result.
To create a new shape you will need something like this. You just will
need to change the geometry type to adapt it to your needs:
(Shape with a schema based on a existing layer)
layer = gvsig.currentLayer()
features = layer.features()
#Create schema based on a existing schema
sch = gvsig.createFeatureType(layer.getSchema())
#We modified the geometry field of that schema for the geometry type that we want
sch.getDefaultGeometryAttribute().setGeometryType(geom.POINT, geom.D3)
#new shape
shp = gvsig.createShape(sch, prefixname="points3d")
(New shape with new schema)
#Create schema based on a existing schema
sch = gvsig.createFeatureType(layer.getSchema())
sch.append("ID", "INTEGER", 10)
sch.append("GEOMETRY", "GEOMETRY")
#We modified the geometry field of that schema for the geometry type that we want
sch.getDefaultGeometryAttribute().setGeometryType(geom.POINT, geom.D3)
#new shape
shp = gvsig.createShape(sch, prefixname="points3d")
In this shape (that should be in editing mode because you just create
it), you could directly append values, commit the changes, and add the
layer to the View
...
shp.append(values)
shp.commit()
gvsig.currentView().addLayer(shp)
El 14/10/16 a las 19:21, Silvia Franceschi escribió:
>
> I need to export the results of my elaborations of intersection in a
> new file, possibly not overwrite the old one.
>
> Thanks
>
> Silvia
>
>
> On Oct 14, 2016 4:29 PM, "Óscar Martínez" <omartinez at gvsig.com
> <mailto:omartinez at gvsig.com>> wrote:
>
> No problem at all! :)
>
> Sorry, i'm not sure what do you want to do.. create a new
> shapefile and work with it? or export (make a copy) of a existing
> shapefile?
>
>
> El 14/10/16 a las 15:33, Silvia Franceschi escribió:
>> Hi Óscar,
>> always me.. :-)
>> is there also a way to write a layer in a shapefile directly from
>> a script or do I have to load it in gvSIG and save it from there?
>>
>> Thanks
>>
>> Silvia
>>
>>
>> On Fri, Oct 14, 2016 at 1:54 PM, Silvia Franceschi
>> <silvia.franceschi at gmail.com
>> <mailto:silvia.franceschi at gmail.com>> wrote:
>>
>> Thank you Óscar for your detailed explanation and your help!
>>
>> Silvia
>>
>> On Fri, Oct 14, 2016 at 11:59 AM, Óscar Martínez
>> <omartinez at gvsig.com <mailto:omartinez at gvsig.com>> wrote:
>>
>> Reading again your email maybe this also answer your
>> question:
>>
>> Inside the script you can make a: print dir(layer) and it
>> will show you the avalaible functions of that type of object.
>>
>>
>> El 14/10/16 a las 11:26, Óscar Martínez escribió:
>>>
>>> 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.im
>>> <http://org.gvsig.fmap.dal.feature.im>pl.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/>
>>>
>>> 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/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
>>> <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/
>>> <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
>>>> <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>
>> _______________________________________________
>> 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
>> <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>
>
>
> _______________________________________________
> 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/20161019/fb7ae627/attachment.html>
More information about the Gvsig_internacional
mailing list