Create shapefile in scripting

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Create shapefile in scripting

Andrea Antonello
Hi developers,
I looked but I couldn't find the right way for creating a new
shapefile in scripting.

While creating one basing on a temporary file layer is simple, as off docs:

    sch = createFeatureType()
    sch.append("GEOMETRY", "GEOMETRY")
    sch.get("GEOMETRY").setGeometryType(geom.POINT, geom.D2)
    shp = createShape(sch)

I could not find a way to do the same using a defines file path.

Thanks for any hint,
Andrea

_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create shapefile in scripting

Óscar Martínez

Hi Andrea,

At the top of the FLayer documentation, is this info:

createShape(definition[, filename=None, geometryType=None, CRS=None, prefixname="tmpshp"])

Creates a new shape layer. If geometryType is None, will take geometry type from the definition. If parameter geometryType and the geometry type inside the definition are different, raises an error.

Parâmetros:
  • definition – Layer schema :type definition: DefaultFeatureType
  • filename (str) – Full path
  • geometryType 
    type geometryType:
      GeometryType
  • CRS (str) – CRS Code :param str prefixname: first part of the temp name

So it will be something like..

    shp = gvsig.createShape(sch, "/home/osc/temp/2/hr.shp")

You can also use createLayer:
layer = createLayer(schema=schema,
                    servertype="FilesystemExplorer",
                    layertype="Shape",
                    shpFile="/home/osc/temp/test1.shp",
                    CRS="EPSG:25830",
                    geometryType=geom.POINT
                    )
Related: In the next version of gvSIG, it will be possible to use:

sch.setDefaultGeometryType(geom.POINT, geom.D2)

So the script it will be not dependent of the field geometry name (for shape files "GEOMETRY"), so it could be used in any data source.

A small comment:
Anyway, these docs will be updated, there are not so many examples about shape creation (sorry but it takes a lot of time developing these docs). Also, this functions info will appear as javados inside the scripting module, so it will be no problem even if there is no examples about it.

Best regards!

El 12/11/16 a las 08:39, andrea antonello escribió:
Hi developers,
I looked but I couldn't find the right way for creating a new
shapefile in scripting.

While creating one basing on a temporary file layer is simple, as off docs:

    sch = createFeatureType()
    sch.append("GEOMETRY", "GEOMETRY")
    sch.get("GEOMETRY").setGeometryType(geom.POINT, geom.D2)
    shp = createShape(sch)

I could not find a way to do the same using a defines file path.

Thanks for any hint,
Andrea

_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel


_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create shapefile in scripting

Andrea Antonello
Hi Oscar,
fantastic answer, thank you.

Can I ask you where you picked the docs you are linking? I will be more than happy to look it up by myself without bothering you :-).

Thanks,
Andrea



On Sat, Nov 12, 2016 at 8:59 AM, Óscar Martínez <[hidden email]> wrote:

Hi Andrea,

At the top of the FLayer documentation, is this info:

createShape(definition[, filename=None, geometryType=None, CRS=None, prefixname="tmpshp"])

Creates a new shape layer. If geometryType is None, will take geometry type from the definition. If parameter geometryType and the geometry type inside the definition are different, raises an error.

Parâmetros:
  • definition – Layer schema :type definition: DefaultFeatureType
  • filename (str) – Full path
  • geometryType 
    type geometryType:
      GeometryType
  • CRS (str) – CRS Code :param str prefixname: first part of the temp name

So it will be something like..

    shp = gvsig.createShape(sch, "/home/osc/temp/2/hr.shp")

You can also use createLayer:
layer = createLayer(schema=schema,
                    servertype="FilesystemExplorer",
                    layertype="Shape",
                    shpFile="/home/osc/temp/test1.shp",
                    CRS="EPSG:25830",
                    geometryType=geom.POINT
                    )
Related: In the next version of gvSIG, it will be possible to use:

sch.setDefaultGeometryType(geom.POINT, geom.D2)

So the script it will be not dependent of the field geometry name (for shape files "GEOMETRY"), so it could be used in any data source.

A small comment:
Anyway, these docs will be updated, there are not so many examples about shape creation (sorry but it takes a lot of time developing these docs). Also, this functions info will appear as javados inside the scripting module, so it will be no problem even if there is no examples about it.

Best regards!

El 12/11/16 a las 08:39, andrea antonello escribió:
Hi developers, I looked but I couldn't find the right way for creating a new shapefile in scripting. While creating one basing on a temporary file layer is simple, as off docs: sch = createFeatureType() sch.append("GEOMETRY", "GEOMETRY") sch.get("GEOMETRY").
setGeometryType(geom.POINT, geom.D2) shp = createShape(sch) I could not find a way to do the same using a defines file path. Thanks for any hint, Andrea _______________________________________________ gvSIG-desktop-devel mailing list http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel


_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel


_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create shapefile in scripting

Andrea Antonello
Hi Oscar,
I finally tried the code you propose, but am having a few issues.

The following:

  shpPath = "/home/moovida/TMP/test.shp"
  epsg = "EPSG:4326"

  schema = createFeatureType()
  schema.append("id", "INTEGER", 4)
  schema.append("name", "STRING", 20)
  schema.append("GEOMETRY", "GEOMETRY")
  schema.get("GEOMETRY").setGeometryType(POINT, D2)

  shape = createShape(schema, filename=shpPath, CRS=epsg)

  print "name: ", shape.getName(), ", path: ", shape.getDataStore().getFullName()

should work following the API you pasted. But it is giving me a runtime exception, without any information about the reason.

Do you see an error in my code that I didn't?

Thanks,
Andrea






On Sat, Nov 12, 2016 at 10:02 AM, andrea antonello <[hidden email]> wrote:
Hi Oscar,
fantastic answer, thank you.

Can I ask you where you picked the docs you are linking? I will be more than happy to look it up by myself without bothering you :-).

Thanks,
Andrea



On Sat, Nov 12, 2016 at 8:59 AM, Óscar Martínez <[hidden email]> wrote:

Hi Andrea,

At the top of the FLayer documentation, is this info:

createShape(definition[, filename=None, geometryType=None, CRS=None, prefixname="tmpshp"])

Creates a new shape layer. If geometryType is None, will take geometry type from the definition. If parameter geometryType and the geometry type inside the definition are different, raises an error.

Parâmetros:
  • definition – Layer schema :type definition: DefaultFeatureType
  • filename (str) – Full path
  • geometryType 
    type geometryType:
      GeometryType
  • CRS (str) – CRS Code :param str prefixname: first part of the temp name

So it will be something like..

    shp = gvsig.createShape(sch, "/home/osc/temp/2/hr.shp")

You can also use createLayer:
layer = createLayer(schema=schema,
                    servertype="FilesystemExplorer",
                    layertype="Shape",
                    shpFile="/home/osc/temp/test1.shp",
                    CRS="EPSG:25830",
                    geometryType=geom.POINT
                    )
Related: In the next version of gvSIG, it will be possible to use:

sch.setDefaultGeometryType(geom.POINT, geom.D2)

So the script it will be not dependent of the field geometry name (for shape files "GEOMETRY"), so it could be used in any data source.

A small comment:
Anyway, these docs will be updated, there are not so many examples about shape creation (sorry but it takes a lot of time developing these docs). Also, this functions info will appear as javados inside the scripting module, so it will be no problem even if there is no examples about it.

Best regards!

El 12/11/16 a las 08:39, andrea antonello escribió:
Hi developers, I looked but I couldn't find the right way for creating a new shapefile in scripting. While creating one basing on a temporary file layer is simple, as off docs: sch = createFeatureType() sch.append("GEOMETRY", "GEOMETRY") sch.get("GEOMETRY").
setGeometryType(geom.POINT, geom.D2) shp = createShape(sch) I could not find a way to do the same using a defines file path. Thanks for any hint, Andrea _______________________________________________ gvSIG-desktop-devel mailing list http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel


_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel



_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create shapefile in scripting

Óscar Martínez

Hi,

The docs usually are here: http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/index.html

But seems like the website it's not working.. so check them here (maybe is not the final version, but almost) : https://oscar9.github.io/gvsig_scripting/es/scripting_devel_guide/2.3/index.html

Some bugs appear in the console, but even if it doesnt, in your case, probably error is the file already exists. Small script with some checks:

# encoding: utf-8

from gvsig import *
from gvsig.geom import *
import os

def main(*args):

  shpPath = "/home/osc/temp/test.shp"
  epsg = "EPSG:4326"

  schema = createFeatureType()
  schema.append("id", "INTEGER", 4)
  schema.append("name", "STRING", 20)
  schema.append("GEOMETRY", "GEOMETRY")
  schema.get("GEOMETRY").setGeometryType(POINT, D2)

  if os.path.exists(shpPath):
      print "Already exist"
      return

  if not os.path.exists(os.path.dirname(shpPath)):
      print "doesnt exist"
      return
     
     
  shape = createShape(schema, filename=shpPath, CRS=epsg)

If it doesn't work, try to see the console/log

Best regards!


El 12/11/16 a las 11:19, andrea antonello escribió:
Hi Oscar,
I finally tried the code you propose, but am having a few issues.

The following:

  shpPath = "/home/moovida/TMP/test.shp"
  epsg = "EPSG:4326"

  schema = createFeatureType()
  schema.append("id", "INTEGER", 4)
  schema.append("name", "STRING", 20)
  schema.append("GEOMETRY", "GEOMETRY")
  schema.get("GEOMETRY").setGeometryType(POINT, D2)

  shape = createShape(schema, filename=shpPath, CRS=epsg)

  print "name: ", shape.getName(), ", path: ", shape.getDataStore().getFullName()

should work following the API you pasted. But it is giving me a runtime exception, without any information about the reason.

Do you see an error in my code that I didn't?

Thanks,
Andrea






On Sat, Nov 12, 2016 at 10:02 AM, andrea antonello <[hidden email]> wrote:
Hi Oscar,
fantastic answer, thank you.

Can I ask you where you picked the docs you are linking? I will be more than happy to look it up by myself without bothering you :-).

Thanks,
Andrea



On Sat, Nov 12, 2016 at 8:59 AM, Óscar Martínez <[hidden email]> wrote:

Hi Andrea,

At the top of the FLayer documentation, is this info:

createShape(definition[, filename=None, geometryType=None, CRS=None, prefixname="tmpshp"])

Creates a new shape layer. If geometryType is None, will take geometry type from the definition. If parameter geometryType and the geometry type inside the definition are different, raises an error.

Parâmetros:
  • definition – Layer schema :type definition: DefaultFeatureType
  • filename (str) – Full path
  • geometryType 
    type geometryType:
      GeometryType
  • CRS (str) – CRS Code :param str prefixname: first part of the temp name
So it will be something like..

    shp = gvsig.createShape(sch, "/home/osc/temp/2/hr.shp")

You can also use createLayer:
layer = createLayer(schema=schema,
                    servertype="FilesystemExplorer",
                    layertype="Shape",
                    shpFile="/home/osc/temp/test1.shp",
                    CRS="EPSG:25830",
                    geometryType=geom.POINT
                    )
Related: In the next version of gvSIG, it will be possible to use: sch.setDefaultGeometryType(geom.POINT, geom.D2) So the script it will be not dependent of the field geometry name (for shape files "GEOMETRY"), so it could be used in any data source. A small comment: Anyway, these docs will be updated, there are not so many examples about shape creation (sorry but it takes a lot of time developing these docs). Also, this functions info will appear as javados inside the scripting module, so it will be no problem even if there is no examples about it. Best regards!
El 12/11/16 a las 08:39, andrea antonello escribió:
Hi developers, I looked but I couldn't find the right way for creating a new shapefile in scripting. While creating one basing on a temporary file layer is simple, as off docs: sch = createFeatureType() sch.append("GEOMETRY", "GEOMETRY") sch.get("GEOMETRY").
setGeometryType(geom.POINT, geom.D2) shp = createShape(sch) I could not find a way to do the same using a defines file path. Thanks for any hint, Andrea _______________________________________________ gvSIG-desktop-devel mailing list http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
_______________________________________________ gvSIG-desktop-devel mailing list http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel

_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create shapefile in scripting

Andrea Antonello

The docs usually are here: http://downloads.gvsig.org/download/web/html/es/scripting_devel_guide/2.3/index.html

But seems like the website it's not working.. so check them here (maybe is not the final version, but almost) : https://oscar9.github.io/gvsig_scripting/es/scripting_devel_guide/2.3/index.html


Yes, I am using those pages constantly :-) But I didn't see that part you pasted, I thought it was some sort of javadoc.

 

Some bugs appear in the console, but even if it doesnt, in your case, probably error is the file already exists. Small script with some checks:

# encoding: utf-8

from gvsig import *
from gvsig.geom import *
import os

def main(*args):

  shpPath = "/home/osc/temp/test.shp"
  epsg = "EPSG:4326"

  schema = createFeatureType()
  schema.append("id", "INTEGER", 4)
  schema.append("name", "STRING", 20)
  schema.append("GEOMETRY", "GEOMETRY")
  schema.get("GEOMETRY").setGeometryType(POINT, D2)

  if os.path.exists(shpPath):
      print "Already exist"
      return

  if not os.path.exists(os.path.dirname(shpPath)):
      print "doesnt exist"
      return
     
     
  shape = createShape(schema, filename=shpPath, CRS=epsg)

If it doesn't work, try to see the console/log


It was working, I was writing the path wrong and was looking in the wrong places. Thanks man!

Andrea

 

Best regards!


El 12/11/16 a las 11:19, andrea antonello escribió:
Hi Oscar,
I finally tried the code you propose, but am having a few issues.

The following:

  shpPath = "/home/moovida/TMP/test.shp"
  epsg = "EPSG:4326"

  schema = createFeatureType()
  schema.append("id", "INTEGER", 4)
  schema.append("name", "STRING", 20)
  schema.append("GEOMETRY", "GEOMETRY")
  schema.get("GEOMETRY").setGeometryType(POINT, D2)

  shape = createShape(schema, filename=shpPath, CRS=epsg)

  print "name: ", shape.getName(), ", path: ", shape.getDataStore().getFullName()

should work following the API you pasted. But it is giving me a runtime exception, without any information about the reason.

Do you see an error in my code that I didn't?

Thanks,
Andrea






On Sat, Nov 12, 2016 at 10:02 AM, andrea antonello <[hidden email]> wrote:
Hi Oscar,
fantastic answer, thank you.

Can I ask you where you picked the docs you are linking? I will be more than happy to look it up by myself without bothering you :-).

Thanks,
Andrea



On Sat, Nov 12, 2016 at 8:59 AM, Óscar Martínez <[hidden email]> wrote:

Hi Andrea,

At the top of the FLayer documentation, is this info:

createShape(definition[, filename=None, geometryType=None, CRS=None, prefixname="tmpshp"])

Creates a new shape layer. If geometryType is None, will take geometry type from the definition. If parameter geometryType and the geometry type inside the definition are different, raises an error.

Parâmetros:
  • definition – Layer schema :type definition: DefaultFeatureType
  • filename (str) – Full path
  • geometryType 
    type geometryType:
      GeometryType
  • CRS (str) – CRS Code :param str prefixname: first part of the temp name
So it will be something like..

    shp = gvsig.createShape(sch, "/home/osc/temp/2/hr.shp")

You can also use createLayer:
layer = createLayer(schema=schema,
                    servertype="FilesystemExplorer",
                    layertype="Shape",
                    shpFile="/home/osc/temp/test1.shp",
                    CRS="EPSG:25830",
                    geometryType=geom.POINT
                    )
Related: In the next version of gvSIG, it will be possible to use: sch.setDefaultGeometryType(geom.POINT, geom.D2) So the script it will be not dependent of the field geometry name (for shape files "GEOMETRY"), so it could be used in any data source. A small comment: Anyway, these docs will be updated, there are not so many examples about shape creation (sorry but it takes a lot of time developing these docs). Also, this functions info will appear as javados inside the scripting module, so it will be no problem even if there is no examples about it. Best regards!
El 12/11/16 a las 08:39, andrea antonello escribió:
Hi developers, I looked but I couldn't find the right way for creating a new shapefile in scripting. While creating one basing on a temporary file layer is simple, as off docs: sch = createFeatureType() sch.append("GEOMETRY", "GEOMETRY") sch.get("GEOMETRY").
setGeometryType(geom.POINT, geom.D2) shp = createShape(sch) I could not find a way to do the same using a defines file path. Thanks for any hint, Andrea _______________________________________________ gvSIG-desktop-devel mailing list http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
_______________________________________________ gvSIG-desktop-devel mailing list http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel
_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel

_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel


_______________________________________________
gvSIG-desktop-devel mailing list
http://joinup.ec.europa.eu/mailman/listinfo/gvsig-desktop-devel