Hot answers tagged

60

read your shapefile with Fiona, PyShp, ogr or ...using the geo_interface protocol (GeoJSON): with Fiona import fiona shape = fiona.open("my_shapefile.shp") print shape.schema {'geometry': 'LineString', 'properties': OrderedDict([(u'FID', 'float:11')])} #first feature of the shapefile first = shape.next() print first # (GeoJSON format) {'geometry':...


30

I find geopandas as the best performer here. Code: import geopandas as gpd shapefile = gpd.read_file("shapefile.shp") print(shapefile)


27

You must first understand how PyQGIS handles geometry (Geometry Handling) The most important element is the point: QgsPoint(x,y) and a line or a segment of line are composed of two points: QgsGeometry.fromPolyline([QgsPoint(x1,y1),QgsPoint(x2,y2)])); So to construct a line: line_start = QgsPoint(50,50) line_end = QgsPoint(100,150) line = ...


27

The simplest way is to use the points to create the line. To do this in QGIS use the Points2One plugin. Install this plugin (available via Plugins->Manage and install plugins) and follow the dialog to create a line from your points. If you have multiple lines, then make sure your points data has a suitable ID field to identify the lines they belong to....


24

There is a QGIS plugin called Digitizing Tools: The documentation says: Split selected features with selected line(s) from another layer applies to: line and polygon layer (multi or single part) Splits all selected features of the active layer with the selected line features of another layer. The splitting creates new features (not multi features). Each ...


23

Explode lines from the Processing Toolbox will split all lines at their vertices to separate lines. Menu Processing -> Toolbox. Type "explode" into the search field to find the function. Select your line layer from the dropdown and start. This will produce a new layer. Selecting one or more lines from the line layer will produce a splitted layer ...


22

The nodes: You want two things, the end points of the polylines (without intermediate nodes) and the intersection points. There are an additional problem, some polylines end points are also intersection points: A solution is to use Python and the modules Shapely and Fiona 1) Read the shapefile: from shapely.geometry import Point, shape import fiona lines ...


21

There is now a plugin called QChainage that does exactly what you want. You specify an interval distance and the plugin creates points along your line at the specified interval.


21

The extract function is behaving exactly as it should. You can force the crop function to use the extent of the polygon and then mask the object to return the exact raster representing the polygon area. If you continue to receive the error it means that your data, in fact, does not overlap. Please keep in mind that R does not perform "on the fly" ...


21

As an alternative, you could: Use the Convert Lines to Points tool from: Processing Toolbox > SAGA > Shapes - Points > Convert Lines to Points (Add points over small distances. E.g. add a point every 1m if the overall line is 100m) Use the Distance to nearest hub from: Processing Toolbox > QGIS geoalgorithms > Vector analysis tools > Distance to nearest ...


21

Simplest way is to style the line. Add a new symbol layer to your line and change the 'Symbol Layer Type' to marker line. Select 'Marker' in the style tree and select a triangle as your default marker symbol. I believe 'triangle 2' is your best bet. Accept these changes and all your lines will now have arrow showing the direction of the line. You can mess ...


21

Short answer: you can get it using a custom SVG. See bottom of this post for one. Long answer: I believe it is better to represent it than to modify the line geometry. Should you want to move an edge or do other actions on the geometry, it would be a nightmare to manage if the wiggles are part of the geometry instead of just a representation of a straight ...


20

In the Python console: you can use the Shapely module (as in How to create equidistant points in QGIS?) with the function point = line.interpolate(currentdistance) The new Python API of the master version (1.9) has an equivalent command (as in Generating chainage (distance) nodes in QGIS of Nathan Woodrow) point = geom.interpolate(currentdistance) or ...


20

To create polygons from points, you can also use "points to path", then "lines to polygons" tools without need to install any plugin


20

You are going about it the right way, using ST_PointN and generate_series. One way of doing this, using dummy data (substitute your own in initial CTE) would be: WITH sample(geom, id) AS (VALUES (ST_MakePoint(0,0), 1), (ST_MakeLine( ARRAY[ST_MakePoint(0, 0), ST_MakePoint(10,10), ST_MakePoint(50, 50), ...


19

This query should do the trick: WITH line AS (SELECT your_polylinestring_id, (ST_Dump(geom)).geom AS geom FROM your_polylinestring_table), linemeasure AS (SELECT ST_AddMeasure(line.geom, 0, ST_Length(line.geom)) AS linem, generate_series(0, ST_Length(line.geom)::int, 10) AS i FROM line), geometries AS ( ...


19

I propose a solution using PyQGIS. It should work both for Linestring and MultiLineString layers. This solution is based on the creation of semicircular rings, so you need to set a value for the diameter (i.e. the step variable in the code below). The step you choose won't be the real step used because it is adjusted on the basis of the line length (but it ...


18

The Polygonize tool from the Processing toolbox still works fine for me on simple lines: It is however necessary that the lines intersect, or share common vertices. And lines should have no self-intersection. You might need to snap the corner vertices to get it working. I usually set the snapping tolerance to 10 pixels to the vertex.


17

From the QGIS Documents: Geometry Handling, you can use the following code to get the length of any selected line(s): layer = qgis.utils.iface.activeLayer() features = layer.selectedFeatures() for f in features: geom = f.geometry() print "Length:", geom.length()


16

Following on from the comments, here's a version that works with perpendicular line segments. Please use with caution as I haven't tested it thoroughly! This method is much more clunky than @whuber's answer - partly because I'm not a very good programmer, and partly because the vector processing is a bit of a faff. I hope it'll at least get you started if ...


16

Yes you can. Go to 'layer properties' > 'labels' Under 'placement' choose 'Using perimeter'


15

You need to use Symbol Levels: The higher the number the later is it drawn. So black will be rendered first then the purple over top meaning that any black bits will be rendered over.


15

Yes, but sort of. ArcGis no longer has line-node topology that enables the user to tell how many arcs (lines) are connected at their ends (nodes). To check is one thing, but how about to fix instead? If you open the feature class in ArcMap and then use planarize lines (give a tolerance) and the lines will be snapped and split at intersection - saves a lot ...


15

ST_Polygonize will do the job: CREATE VIEW boundarypolygons AS SELECT g.path[1] as gid, g.geom::geometry(polygon, 31492) as geom FROM (SELECT (ST_Dump(ST_Polygonize(geom))).* FROM boundary ) as g;


15

use QgsGeometry.transform( QgsCoordinateTransform tr ). for example after created your instance of QgsCoordinateTransform with source and dest crs, for each geometry instance do: sourceCrs = QgsCoordinateReferenceSystem(4326) destCrs = QgsCoordinateReferenceSystem(2154) tr = QgsCoordinateTransform(sourceCrs, destCrs, QgsProject.instance()) ...


15

There is a possibility using a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... Let's assume there is a polyline layer called 'polylines' (blue lines). With the following query, it is possible to create a point at the end of each line. SELECT st_endpoint(geometry), * FROM "polylines" The output point layer (...


14

I agree with Barbarossa that accessing the power of the da module would be beneficial. Here is very clean scripting approach: import arcpy fc = r'C:\Users\OWNER\Documents\ArcGIS\Default.gdb\samplePolyline' fields = ['x1','x2','y1','y2'] # Add fields to your FC for field in fields: arcpy.AddField_management(fc,str(field),"DOUBLE") with arcpy.da....


14

Your line is made of several segments that have different orientations, so the "right" side of each segment is not always on the same side of the complete line. To avoid this, you can first dissolve the line segments so that you are effectively buffing a single line. Alternatively, you can identify and swap direction of some segments.


13

Posted a code snippet(tested in python console) that doest the below Use QgsSpatialIndex to find the nearest line feature to a point Find the nearest point on this line to the point. I used shapely package as a shortcut for this. I found the QGis methods for this as insufficient(or most probably i do not understand them properly) Added rubberbands to the ...


13

Try dissolving based off of OBJECTID or FID. Then use Feature To Polygon (Data Management) to convert your polylines to polygons if you wish. To illustrate, here are some sample lines: Attribute table before dissolve by OBJECTID Attribute table after dissolve by OBJECTID


Only top voted, non community-wiki answers of a minimum length are eligible