Index: AbstractVectorialLegend.java
===================================================================
--- AbstractVectorialLegend.java	(revision 42463)
+++ AbstractVectorialLegend.java	(revision 42464)
@@ -87,15 +87,15 @@
 
 /**
  * Base implementation for Vectorial data Legends.
- * 
+ *
  * Provides a draw method implementation which loads the {@link Feature}s and
  * uses the {@link ISymbol} objects to draw the {@link Geometry} objects.
- * 
+ *
  * @author 2009- <a href="cordinyana@gvsig.org">César Ordiñana</a> - gvSIG team
  */
 public abstract class AbstractVectorialLegend extends AbstractLegend implements
 IVectorLegend {
-	
+
 	private static final Logger LOG = LoggerFactory
 			.getLogger(AbstractVectorialLegend.class);
 
@@ -123,7 +123,7 @@
         this.zSort = zSort;
         addLegendListener(zSort);
     }
-    
+
     @SuppressWarnings("unchecked")
     public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
         Cancellable cancel, double scale, Map queryParameters,
@@ -133,7 +133,7 @@
         draw(image, g, viewPort, cancel, scale, queryParameters, coordTrans,
             featureStore, null, dpi);
     }
-    
+
     @SuppressWarnings("unchecked")
     public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
         Cancellable cancel, double scale, Map queryParameters,
@@ -152,7 +152,7 @@
         print(g, viewPort, cancel, scale, queryParameters, coordTrans,
             featureStore, null, properties);
     }
-    
+
     @SuppressWarnings("unchecked")
     public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
         double scale, Map queryParameters, ICoordTrans coordTrans,
@@ -178,7 +178,7 @@
 
             int mapLevelCount = (useZSort) ? zSort.getLevelCount() : 1;
             for (int mapPass = 0; mapPass < mapLevelCount; mapPass++) {
-                
+
                 Envelope vp_env_in_store_crs = null;
                 IProjection store_crs = null;
                 if (coordTrans != null) {
@@ -191,7 +191,7 @@
                     vp_env_in_store_crs = viewPort.getAdjustedEnvelope();
                     store_crs = viewPort.getProjection();
                 }
-                
+
                 FeatureQuery feat_query = fquery;
                 Envelope store_env = featureStore.getEnvelope();
                 boolean use_intersection_cond = false;
@@ -205,9 +205,9 @@
                         use_intersection_cond = true;
                     }
                 }
-                
+
                 if (use_intersection_cond) {
-                    FeatureType ft = featureStore.getDefaultFeatureType(); 
+                    FeatureType ft = featureStore.getDefaultFeatureType();
                     IntersectsEnvelopeEvaluator iee =
                         new IntersectsEnvelopeEvaluator(
                             vp_env_in_store_crs,
@@ -220,7 +220,7 @@
                     }
                     feat_query.addFilter(iee);
                 }
-                
+
                 // 'feat_query' can still be NULL here, so we only filter
                 // the featureStore if it's not null
                 if (feat_query == null) {
@@ -235,7 +235,7 @@
                     Geometry geom = feat.getDefaultGeometry();
                     if (geom==null) {
                     	continue;
-                    }                    
+                    }
                     // Reprojection if needed
                     if (coordTrans != null) {
                         geom = geom.cloneGeometry();
@@ -363,9 +363,13 @@
 		}
 
 		IProjection dataProjection;
-		Envelope reprojectedDataEnvelop;
-
+		Envelope dataEnvelope;
+		Envelope reprojectedDataEnvelope;
+		// Gets the view envelope
+		Envelope viewPortEnvelope = viewPort.getAdjustedEnvelope();
+        Envelope reprojectedViewPortEnvelope;
 		try {
+		    dataEnvelope = featureStore.getEnvelope();
 			if (coordTrans == null) {
 				dataProjection = featureStore.getDefaultFeatureType()
 						.getDefaultSRS();
@@ -376,37 +380,42 @@
 					dataProjection = viewPort.getProjection();
 				}
 
-				reprojectedDataEnvelop = featureStore.getEnvelope();
+				reprojectedDataEnvelope = dataEnvelope;
+                reprojectedViewPortEnvelope = viewPortEnvelope;
 			} else {
 				dataProjection = coordTrans.getPOrig();
 
-				Envelope env = featureStore.getEnvelope();
-				if ( env!=null && !env.isEmpty()) {
-					reprojectedDataEnvelop = env.convert(coordTrans);
+				if ( dataEnvelope!=null && !dataEnvelope.isEmpty()) {
+					reprojectedDataEnvelope = dataEnvelope.convert(coordTrans);
 				} else {
-					reprojectedDataEnvelop = env;
+					reprojectedDataEnvelope = dataEnvelope;
 				}
+                if ( viewPortEnvelope!=null && !viewPortEnvelope.isEmpty()) {
+                    reprojectedViewPortEnvelope = viewPortEnvelope.convert(coordTrans.getInverted());
+                } else {
+                    reprojectedViewPortEnvelope = viewPortEnvelope;
+                }
 			}
 		} catch (DataException e) {
 			throw new LegendDrawingException(e);
 		}
 
-		// Gets the view envelope
-		Envelope viewPortEnvelope = viewPort.getAdjustedEnvelope();
 
 		// Gets the data envelope with the viewport SRS
-		Envelope myEnvelope = reprojectedDataEnvelop;
+//		Envelope myEnvelope = reprojectedDataEnvelope;
 
 		// TODO: in some cases, the legend may need a different check to
 		// decide if the data must be drawn or not
 		// Checks if the viewport envelope intersects with the data envelope
-		if (!viewPortEnvelope.intersects(myEnvelope)) {
+		// This condition may seem redundant, but sometimes the transformations may fail and cause false negatives.
+		if (!viewPortEnvelope.intersects(reprojectedDataEnvelope) && !(reprojectedViewPortEnvelope.intersects(dataEnvelope))) {
 			// The data is not visible in the current viewport, do nothing.
 			return;
 		}
 
 		// Check if all the data is contained into the viewport envelope
-		boolean containsAll = viewPortEnvelope.contains(myEnvelope);
+        // This condition may seem redundant, but sometimes the transformations may fail and cause false negatives.
+		boolean containsAll = viewPortEnvelope.contains(reprojectedDataEnvelope) || reprojectedViewPortEnvelope.contains(dataEnvelope);
 
 		// Create the drawing notification to be reused on each iteration
 		DefaultFeatureDrawnNotification drawnNotification = new DefaultFeatureDrawnNotification();
@@ -463,13 +472,13 @@
         boolean containsAll) throws DataException {
 
         featureQuery.setScale(scale);
-        
+
         //Adds the attributes
         String[] fieldNames = getRequiredFeatureAttributeNames(featureStore);
         for (int i=0 ; i<fieldNames.length ;i++){
             featureQuery.addAttributeName(fieldNames[i]);
-        }       
-        
+        }
+
         // TODO: Mobile has it's own IntersectsEnvelopeEvaluator
         if (!containsAll) {
             // Gets the viewport envelope with the data SRS
@@ -490,7 +499,7 @@
                 featureStore.getDefaultFeatureType(), featureStore
                 .getDefaultFeatureType()
                 .getDefaultGeometryAttributeName());
-            featureQuery.addFilter(iee);			
+            featureQuery.addFilter(iee);
         }
         if (queryParameters != null) {
             Iterator iterEntry = queryParameters.entrySet().iterator();
@@ -500,7 +509,7 @@
                 featureQuery.setQueryParameter((String) entry.getKey(),
                     entry.getValue());
             }
-        }	
+        }
         return featureQuery;
     }
 
@@ -579,14 +588,17 @@
             try {
                 geom.reProject(coordTrans);
             } catch (ReprojectionRuntimeException re) {
+                LOG.warn("Can't reproject geometry "+geom.toString(), re);
+                return;
+
                 /*
                  * Library was unable to reproject
                  * See reproject method in Point2D (geometry)
                  */
-                throw new CreateGeometryException(
-                    geom.getGeometryType().getType(),
-                    geom.getGeometryType().getSubType(),
-                    re);
+//                throw new CreateGeometryException(
+//                    geom.getGeometryType().getType(),
+//                    geom.getGeometryType().getSubType(),
+//                    re);
             }
         }
 
@@ -926,7 +938,7 @@
     /**
      * Returns the names of the {@link Feature} attributes required for the
      * {@link ILegend} to operate.
-     * 
+     *
      * @param featureStore
      *            the store where the {@link Feature}s belong to
      * @return the names of required {@link Feature} attribute names
