1

Using shapely, I've got a LINEARRING object (obj) that defines the boundary of the CONUS:

obj.exterior

LINEARRING (-123.489 49.076, -122.961 49.023, -122.821 49.015, -119.285 49.014, -117.695 49.046, -116.207 49.019, -114.443 49.007, -111.438 49.009, -108.955 49.044, -107.31 48.993, -105.605 49.009, -103.573 48.991, -101.079 48.988, -99.687 49.004, -98.242 48.996, -96.798 48.991, -95.873 48.998, -95.10599999999999 48.998, -95.193 49.339, -94.879 49.359, -94.623 48.797, -93.95 48.642, -93.64100000000001 48.521, -93.202 48.624, -92.611 48.546, -92.04900000000001 48.36, -91.504 48.094, -90.83499999999999 48.281, -90.646 48.094, -90.054 48.116, -89.687 48.017, -88.373 48.302, -87.25 47.869, -85.896 47.31, -84.848 46.885, -84.70699999999999 46.516, -84.142 46.508, -83.98 45.974, -82.907 45.52, -82.52800000000001 45.336, -82.199 44.145, -82.152 43.755, -82.166 43.211, -82.267 43.077, -82.45399999999999 42.997, -82.483 42.619, -82.70399999999999 42.431, -82.729 42.36, -83.068 42.27, -83.18899999999999 42.064, -83.08 41.93, -83.10299999999999 41.858, -82.7 41.693, -82.31999999999999 41.712, -81.62 42.066, -81.185 42.216, -80.167 42.399, -79.35299999999999 42.673, -78.96599999999999 42.881, -79.194 43.433, -78.59699999999999 43.655, -77.369 43.615, -76.813 43.628, -76.377 44.151, -75.828 44.443, -75.494 44.756, -74.876 45.025, -71.551 45.021, -71.366 45.294, -70.884 45.379, -70.38 45.841, -70.319 46.114, -70.03 46.658, -69.251 47.478, -68.967 47.467, -68.967 47.24, -68.497 47.296, -68.28700000000001 47.417, -67.785 47.096, -67.744 46.598, -67.724 45.726, -67.40300000000001 45.548, -67.372 45.222, -67.182 45.297, -67.04300000000001 45.044, -66.98 44.805, -67.125 44.219, -67.523 44.075, -67.926 43.878, -68.39100000000001 43.742, -68.648 43.687, -68.934 43.577, -69.42100000000001 43.41, -69.812 43.232, -70.081 42.985, -70.05200000000001 42.771, -69.71299999999999 42.265, -69.508 42.027, -69.42400000000001 41.646, -69.53 41.355, -69.488 41.166, -69.697 40.974, -69.91 40.904, -70.321 40.939, -71.09999999999999 40.903, -71.55200000000001 40.783, -72.23699999999999 40.579, -72.958 40.325, -73.608 40.251, -73.72799999999999 39.856, -73.907 39.442, -74.166 39.102, -74.401 38.961, -74.563 38.755, -74.73999999999999 38.581, -74.754 38.236, -74.917 37.91, -75.111 37.663, -75.26000000000001 37.536, -75.401 37.251, -75.53100000000001 36.987, -75.634 36.882, -75.497 36.522, -75.309 36.105, -75.114 35.641, -75.122 35.343, -75.333 34.932, -75.721 34.862, -75.97 34.718, -76.351 34.309, -76.53700000000001 34.266, -76.83 34.353, -77.227 34.217, -77.535 34.015, -77.68600000000001 33.658, -77.928 33.479, -78.23099999999999 33.591, -78.44499999999999 33.55, -78.768 33.349, -78.871 33.088, -79.05500000000001 32.801, -79.27800000000001 32.697, -79.404 32.588, -79.71899999999999 32.422, -79.955 32.235, -80.155 32.145, -80.438 31.934, -80.798 31.584, -80.986 31.069, -81.11 30.563, -80.95099999999999 29.912, -80.754 29.466, -80.383 28.829, -80.196 28.533, -80.26900000000001 28.253, -80.00700000000001 27.674, -79.798 27.178, -79.685 26.743, -79.783 26.079, -79.845 25.421, -80.045 25.048, -80.446 24.6, -80.79000000000001 24.423, -81.17400000000001 24.337, -81.559 24.241, -81.77500000000001 24.23, -81.82599999999999 24.534, -81.827 24.892, -81.824 25.021, -81.628 25.07, -81.485 25.125, -81.49299999999999 25.397, -81.628 25.527, -81.92100000000001 25.564, -82.09999999999999 25.859, -82.196 26.094, -82.416 26.221, -82.562 26.476, -82.69 26.803, -82.812 27.029, -83.04900000000001 27.381, -83.21899999999999 27.851, -83.187 27.993, -83.223 28.199, -83.142 28.392, -82.998 28.572, -83.06399999999999 28.763, -83.251 28.832, -83.508 29.157, -83.72199999999999 29.409, -83.86 29.603, -84.03 29.732, -84.176 29.643, -84.55 29.467, -84.828 29.284, -85.102 29.268, -85.47499999999999 29.368, -85.66500000000001 29.503, -85.762 29.756, -86.22199999999999 30.014, -86.645 30.07, -87.107 30.007, -87.499 29.941, -87.905 29.887, -88.45399999999999 29.885, -88.572 29.608, -88.723 29.407, -88.60599999999999 29.18, -88.88500000000001 28.744, -89.18899999999999 28.623, -89.488 28.586, -89.711 28.762, -89.779 28.956, -90.224 28.739, -90.83 28.701, -91.19 28.833, -91.646 29.135, -91.881 29.055, -92.16800000000001 29.209, -92.544 29.236, -92.938 29.342, -93.196 29.426, -93.553 29.434, -93.717 29.362, -93.995 29.341, -94.468 29.131, -94.788 28.889, -95.20999999999999 28.607, -95.551 28.42, -96.075 28.201, -96.40000000000001 27.946, -96.669 27.752, -96.875 27.541, -96.952 27.384, -97.06999999999999 27.075, -96.962 26.658, -96.85599999999999 26.338, -96.83 25.953, -97.274 25.953, -97.396 25.819, -97.694 26.005, -98.202 26.04, -98.514 26.191, -99.017 26.377, -99.173 26.54, -99.298 26.815, -99.461 27.078, -99.505 27.349, -99.55500000000001 27.605, -99.742 27.695, -100.058 28.071, -100.293 28.287, -100.428 28.64, -100.633 28.891, -100.706 29.121, -100.971 29.366, -101.333 29.608, -101.5 29.752, -101.784 29.79, -102.19 29.778, -102.459 29.789, -102.675 29.671, -102.813 29.337, -102.844 29.159, -103.086 28.972, -103.393 29.005, -104.166 29.392, -104.548 29.649, -104.702 29.898, -104.72 30.256, -104.959 30.538, -105.454 30.916, -105.957 31.291, -106.325 31.547, -106.448 31.769, -107.166 31.772, -107.78 31.773, -108.182 31.777, -108.213 31.305, -109.08 31.308, -109.891 31.327, -110.732 31.321, -111.103 31.32, -112.189 31.67, -113.602 32.098, -114.405 32.336, -114.811 32.476, -114.786 32.682, -115.696 32.617, -116.621 32.551, -117.169 32.529, -117.702 32.47, -117.778 32.446, -117.77 32.813, -117.962 33.043, -118.164 33.198, -118.545 33.073, -118.673 33.067, -119.001 33.474, -119.356 33.836, -119.544 34.032, -119.812 34.037, -120.183 34.221, -120.447 34.466, -120.592 34.565, -121.194 34.573, -121.835 34.561, -122.066 34.917, -122.421 35.277, -122.74 35.559, -122.796 35.653, -122.21 35.655, -121.794 35.651, -121.98 35.891, -122.207 36.036, -122.39 36.508, -122.351 36.672, -122.594 36.88, -122.805 37.087, -122.796 37.214, -122.877 37.376, -123.191 37.378, -123.406 37.535, -123.412 37.845, -123.461 38.119, -123.525 38.275, -123.689 38.42, -124.114 38.743, -124.231 39.053, -124.242 39.626, -124.52 39.956, -124.754 40.106, -124.849 40.358, -124.607 40.95, -124.532 41.476, -124.765 41.966, -124.941 42.537, -125.039 42.866, -124.935 43.082, -124.849 43.38, -124.564 43.847, -124.581 44.518, -124.503 45.107, -124.463 45.583, -124.474 46.093, -124.581 46.213, -124.509 46.463, -124.589 46.739, -124.65 47.149, -124.889 47.473, -125.023 47.729, -125.213 47.948, -125.271 48.102, -125.184 48.484, -125.155 48.566, -123.955 48.401, -123.265 48.385, -123.416 48.856, -123.486 49.072, -123.489 49.076)

I've also got a LineString object (lineobj) that transects the northeast part of the CONUS obj, with beginning/end points beyond the CONUS exterior.

lineobj

LINESTRING (-70.2 42.53, -71.20999999999999 41.64, -72.18000000000001 42.28, -72.12 43.57, -70.95999999999999 44.79, -69.97 45.79, -69.7 47, -68.73 47.75, -66.75 47.97, -65.47 48.03)

How would one create a single polygon (or linearring) by starting at the northernmost lineobj intersection with the CONUS boundary obj, then following the CONUS boundary CW to the southernmost intersection with the lineobj, finally closing along the length of lineobj?

  • Do you possibly have a sketch or plot that you can share? – songololo Nov 6 '16 at 11:28
7

When using shapely, first transform the geometry to a locally appropriate projected coordinate system. I am not that familiar with US appropriate coordinate systems, but let's try epsg 102003.

from shapely import wkt, geometry
import matplotlib.pyplot as plt
import pyproj as proj


# load your geometry from wkt
conus = wkt.loads('LINEARRING (-123.489 49.076, -122.961 49.023, -122.821 49.015, -119.285 49.014, -117.695 49.046, -116.207 49.019, -114.443 49.007, -111.438 49.009, -108.955 49.044, -107.31 48.993, -105.605 49.009, -103.573 48.991, -101.079 48.988, -99.687 49.004, -98.242 48.996, -96.798 48.991, -95.873 48.998, -95.10599999999999 48.998, -95.193 49.339, -94.879 49.359, -94.623 48.797, -93.95 48.642, -93.64100000000001 48.521, -93.202 48.624, -92.611 48.546, -92.04900000000001 48.36, -91.504 48.094, -90.83499999999999 48.281, -90.646 48.094, -90.054 48.116, -89.687 48.017, -88.373 48.302, -87.25 47.869, -85.896 47.31, -84.848 46.885, -84.70699999999999 46.516, -84.142 46.508, -83.98 45.974, -82.907 45.52, -82.52800000000001 45.336, -82.199 44.145, -82.152 43.755, -82.166 43.211, -82.267 43.077, -82.45399999999999 42.997, -82.483 42.619, -82.70399999999999 42.431, -82.729 42.36, -83.068 42.27, -83.18899999999999 42.064, -83.08 41.93, -83.10299999999999 41.858, -82.7 41.693, -82.31999999999999 41.712, -81.62 42.066, -81.185 42.216, -80.167 42.399, -79.35299999999999 42.673, -78.96599999999999 42.881, -79.194 43.433, -78.59699999999999 43.655, -77.369 43.615, -76.813 43.628, -76.377 44.151, -75.828 44.443, -75.494 44.756, -74.876 45.025, -71.551 45.021, -71.366 45.294, -70.884 45.379, -70.38 45.841, -70.319 46.114, -70.03 46.658, -69.251 47.478, -68.967 47.467, -68.967 47.24, -68.497 47.296, -68.28700000000001 47.417, -67.785 47.096, -67.744 46.598, -67.724 45.726, -67.40300000000001 45.548, -67.372 45.222, -67.182 45.297, -67.04300000000001 45.044, -66.98 44.805, -67.125 44.219, -67.523 44.075, -67.926 43.878, -68.39100000000001 43.742, -68.648 43.687, -68.934 43.577, -69.42100000000001 43.41, -69.812 43.232, -70.081 42.985, -70.05200000000001 42.771, -69.71299999999999 42.265, -69.508 42.027, -69.42400000000001 41.646, -69.53 41.355, -69.488 41.166, -69.697 40.974, -69.91 40.904, -70.321 40.939, -71.09999999999999 40.903, -71.55200000000001 40.783, -72.23699999999999 40.579, -72.958 40.325, -73.608 40.251, -73.72799999999999 39.856, -73.907 39.442, -74.166 39.102, -74.401 38.961, -74.563 38.755, -74.73999999999999 38.581, -74.754 38.236, -74.917 37.91, -75.111 37.663, -75.26000000000001 37.536, -75.401 37.251, -75.53100000000001 36.987, -75.634 36.882, -75.497 36.522, -75.309 36.105, -75.114 35.641, -75.122 35.343, -75.333 34.932, -75.721 34.862, -75.97 34.718, -76.351 34.309, -76.53700000000001 34.266, -76.83 34.353, -77.227 34.217, -77.535 34.015, -77.68600000000001 33.658, -77.928 33.479, -78.23099999999999 33.591, -78.44499999999999 33.55, -78.768 33.349, -78.871 33.088, -79.05500000000001 32.801, -79.27800000000001 32.697, -79.404 32.588, -79.71899999999999 32.422, -79.955 32.235, -80.155 32.145, -80.438 31.934, -80.798 31.584, -80.986 31.069, -81.11 30.563, -80.95099999999999 29.912, -80.754 29.466, -80.383 28.829, -80.196 28.533, -80.26900000000001 28.253, -80.00700000000001 27.674, -79.798 27.178, -79.685 26.743, -79.783 26.079, -79.845 25.421, -80.045 25.048, -80.446 24.6, -80.79000000000001 24.423, -81.17400000000001 24.337, -81.559 24.241, -81.77500000000001 24.23, -81.82599999999999 24.534, -81.827 24.892, -81.824 25.021, -81.628 25.07, -81.485 25.125, -81.49299999999999 25.397, -81.628 25.527, -81.92100000000001 25.564, -82.09999999999999 25.859, -82.196 26.094, -82.416 26.221, -82.562 26.476, -82.69 26.803, -82.812 27.029, -83.04900000000001 27.381, -83.21899999999999 27.851, -83.187 27.993, -83.223 28.199, -83.142 28.392, -82.998 28.572, -83.06399999999999 28.763, -83.251 28.832, -83.508 29.157, -83.72199999999999 29.409, -83.86 29.603, -84.03 29.732, -84.176 29.643, -84.55 29.467, -84.828 29.284, -85.102 29.268, -85.47499999999999 29.368, -85.66500000000001 29.503, -85.762 29.756, -86.22199999999999 30.014, -86.645 30.07, -87.107 30.007, -87.499 29.941, -87.905 29.887, -88.45399999999999 29.885, -88.572 29.608, -88.723 29.407, -88.60599999999999 29.18, -88.88500000000001 28.744, -89.18899999999999 28.623, -89.488 28.586, -89.711 28.762, -89.779 28.956, -90.224 28.739, -90.83 28.701, -91.19 28.833, -91.646 29.135, -91.881 29.055, -92.16800000000001 29.209, -92.544 29.236, -92.938 29.342, -93.196 29.426, -93.553 29.434, -93.717 29.362, -93.995 29.341, -94.468 29.131, -94.788 28.889, -95.20999999999999 28.607, -95.551 28.42, -96.075 28.201, -96.40000000000001 27.946, -96.669 27.752, -96.875 27.541, -96.952 27.384, -97.06999999999999 27.075, -96.962 26.658, -96.85599999999999 26.338, -96.83 25.953, -97.274 25.953, -97.396 25.819, -97.694 26.005, -98.202 26.04, -98.514 26.191, -99.017 26.377, -99.173 26.54, -99.298 26.815, -99.461 27.078, -99.505 27.349, -99.55500000000001 27.605, -99.742 27.695, -100.058 28.071, -100.293 28.287, -100.428 28.64, -100.633 28.891, -100.706 29.121, -100.971 29.366, -101.333 29.608, -101.5 29.752, -101.784 29.79, -102.19 29.778, -102.459 29.789, -102.675 29.671, -102.813 29.337, -102.844 29.159, -103.086 28.972, -103.393 29.005, -104.166 29.392, -104.548 29.649, -104.702 29.898, -104.72 30.256, -104.959 30.538, -105.454 30.916, -105.957 31.291, -106.325 31.547, -106.448 31.769, -107.166 31.772, -107.78 31.773, -108.182 31.777, -108.213 31.305, -109.08 31.308, -109.891 31.327, -110.732 31.321, -111.103 31.32, -112.189 31.67, -113.602 32.098, -114.405 32.336, -114.811 32.476, -114.786 32.682, -115.696 32.617, -116.621 32.551, -117.169 32.529, -117.702 32.47, -117.778 32.446, -117.77 32.813, -117.962 33.043, -118.164 33.198, -118.545 33.073, -118.673 33.067, -119.001 33.474, -119.356 33.836, -119.544 34.032, -119.812 34.037, -120.183 34.221, -120.447 34.466, -120.592 34.565, -121.194 34.573, -121.835 34.561, -122.066 34.917, -122.421 35.277, -122.74 35.559, -122.796 35.653, -122.21 35.655, -121.794 35.651, -121.98 35.891, -122.207 36.036, -122.39 36.508, -122.351 36.672, -122.594 36.88, -122.805 37.087, -122.796 37.214, -122.877 37.376, -123.191 37.378, -123.406 37.535, -123.412 37.845, -123.461 38.119, -123.525 38.275, -123.689 38.42, -124.114 38.743, -124.231 39.053, -124.242 39.626, -124.52 39.956, -124.754 40.106, -124.849 40.358, -124.607 40.95, -124.532 41.476, -124.765 41.966, -124.941 42.537, -125.039 42.866, -124.935 43.082, -124.849 43.38, -124.564 43.847, -124.581 44.518, -124.503 45.107, -124.463 45.583, -124.474 46.093, -124.581 46.213, -124.509 46.463, -124.589 46.739, -124.65 47.149, -124.889 47.473, -125.023 47.729, -125.213 47.948, -125.271 48.102, -125.184 48.484, -125.155 48.566, -123.955 48.401, -123.265 48.385, -123.416 48.856, -123.486 49.072, -123.489 49.076)')

lineobj = wkt.loads('LINESTRING (-65 44, -70.2 42.53, -71.20999999999999 41.64, -72.18000000000001 42.28, -72.12 43.57, -70.95999999999999 44.79, -69.97 45.79, -69.7 47, -68.73 47.75, -66.75 47.97, -65.47 48.03)')

# initialise your projections
crs_wgs = proj.Proj(init='epsg:4326')
# using USA contiguous albers equal area projection EPSG 102003
crs_albers = proj.Proj(projparams='+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs', preserve_units=True)

# transform your geometry and create polygons from the x, y arrays
conus_x, conus_y = proj.transform(crs_wgs, crs_albers, conus.xy[0], conus.xy[1])
conus_transformed = geometry.LinearRing([(i[0], i[1]) for i in zip(conus_x, conus_y)])
conus_transformed_poly = geometry.Polygon(conus_transformed)

lineobj_x, lineobj_y = proj.transform(crs_wgs, crs_albers, lineobj.xy[0], lineobj.xy[1])
lineobj_transformed = geometry.LineString([(i[0], i[1]) for i in zip(lineobj_x, lineobj_y)])
lineobj_transformed_poly = geometry.Polygon(lineobj_transformed)

# you can now plot your geometry
plt.plot(conus_transformed_poly.exterior.xy[0], conus_transformed_poly.exterior.xy[1])
plt.plot(lineobj_transformed_poly.exterior.xy[0], lineobj_transformed_poly.exterior.xy[1])
plt.show()

enter image description here

I've taken the liberty of adding an additional point to lineobj because the end of the line string was still within the boundary of conus, and because if we want to create a poly for subsequent operations, we need the resultant polygon to not slice off the easternmost part of the states.

You can now use shapely's difference or intersection methods to get either the difference or intersection of the two. From your question it sounds like you want the intersection operator (plotted green in the map).

conus_difference = conus_transformed_poly.difference(lineobj_transformed_poly)
plt.plot(conus_difference.exterior.xy[0], conus_difference.exterior.xy[1])

conus_intersection = conus_transformed_poly.intersection(lineobj_transformed_poly)
plt.plot(conus_intersection.exterior.xy[0], conus_intersection.exterior.xy[1])

plt.show()

enter image description here

  • 1
    This is exactly what I was trying to get. Thank you very much, I really appreciate the help! – GCarbin Dec 5 '16 at 2:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.